什么会导致部分视图突然找不到?

发布于 2024-08-13 05:58:41 字数 1616 浏览 6 评论 0原文

我很困惑。我的网站随机抛出以下错误:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: The partial view '/SiteUserMenu' could not be found. The following locations were searched:
/SiteUserMenu
   at System.Web.Mvc.HtmlHelper.FindPartialView(ViewContext viewContext, String partialViewName, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(HtmlHelper htmlHelper, String partialViewName)
   at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer)

大多数时候,网站运行良好...有时它会抛出上述错误,这对我来说毫无意义。如果没有找到部分视图,为什么有时会找不到呢?

局部视图保存在共享文件夹(视图文件夹内)下名为“en”的文件夹中。以下是我渲染视图的方式:

<% Html.RenderPartial(ViewData["lang"] + "/SiteUserMenu"); %>

ViewData["lang"] 在我的基本控制器中的 OnActionExecuting 操作中设置,如下所示:

var l = (RouteData.Values["language"] != null) ? RouteData.Values["language"].ToString() : string.Empty;

if (string.IsNullOrEmpty(l))
    l = "en";

if (l.Contains("en"))
{
    IsEnglish = true;
    l = "en";
}
else
    l = "ja";

ViewData["lang"] = l.ToLower();
Language = l.ToLower();

Language 和 IsEnglish 是基本控制器的属性。整个网站使用它们来维护语言选择并根据语言做出适当的选择。如果未设置语言,则会设置为“en”。因此 ViewData["lang"] 应该始终可供我的视图使用。

为什么有时找不到部分视图,而大多数时候(我说的是 90% 的时间)它工作正常?

I'm baffled. My site randomly throws the following error:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: The partial view '/SiteUserMenu' could not be found. The following locations were searched:
/SiteUserMenu
   at System.Web.Mvc.HtmlHelper.FindPartialView(ViewContext viewContext, String partialViewName, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, ViewEngineCollection viewEngineCollection)
   at System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(HtmlHelper htmlHelper, String partialViewName)
   at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer)

Most of the time, the site runs fine... sometimes it throws the above error, which makes no sense to me. If the partial view wasn't found, why would it not be able to find it sometimes?

The partial view is saved in a folder named "en" under the Shared folder (inside of the views folder). Here's how I render the view:

<% Html.RenderPartial(ViewData["lang"] + "/SiteUserMenu"); %>

ViewData["lang"] is set in my base controller, in the OnActionExecuting action, like so:

var l = (RouteData.Values["language"] != null) ? RouteData.Values["language"].ToString() : string.Empty;

if (string.IsNullOrEmpty(l))
    l = "en";

if (l.Contains("en"))
{
    IsEnglish = true;
    l = "en";
}
else
    l = "ja";

ViewData["lang"] = l.ToLower();
Language = l.ToLower();

Language and IsEnglish are properties of the base controller. The whole site uses them to maintain language selection and to make appropriate choices based on language. If language is not set, it will be set to "en". So ViewData["lang"] should always be available to my views.

Any reason why a partial view couldn't be found somtimes, while most of the time (I'm talking 90% of the time) it works fine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-08-20 05:58:41

由于错误指向“无法找到部分视图'/SiteUserMenu'。”,您可以看到它没有选择'en'。我建议开始调试的最佳位置是 ViewData["lang"]。看看您是否可以每次都生成复制错误的步骤,然后调试您的解决方案,看看当您处于控制器的错误 Action 方法中时是否已设置 ViewData["lang"]

As the error points to 'The partial view '/SiteUserMenu' could not be found.' you can see that its not picking up the 'en'. I would suggest that the best place to start debugging is the ViewData["lang"]. See if you can produce steps to replicate the error everytime, then debug into your solution to see if ViewData["lang"] has been set whilst you are in the erroring Action method of your Controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文