开发者

What would cause a partial view to suddenly not be found?

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

System.Web.HttpUnhandledException: Exception of type 'Sys开发者_运维问答tem.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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜