asp.net mvc 5 中在主机上发布时区域混淆的路由属性
Route attributes for areas confusion when published on host in asp.net mvc 5
我正在使用带区域的路线属性。
我的路线配置是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Peacock.Controllers" }
);
routes.MapRoute(
"CMS",
"CMS/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "CMS.Controllers" }
);
}
在我的本地,一切都是正确的!今天,当我发布我的项目并将其上传到我的主机时,我遇到了两种类型的错误。
如果我请求 url 的默认 MapRoute,例如 mysite.com/Contents/1060
,一切都是正确的!但是当我请求我所在区域的 url 时,我遇到了两种类型的错误!
1) 某些请求如 mysite.com/cms/comment
或 mysite.com/cms/category
有此错误:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/CMS/Views/ContentCategory/Index.aspx
~/Areas/CMS/Views/ContentCategory/Index.ascx
~/Areas/CMS/Views/Shared/Index.aspx
~/Areas/CMS/Views/Shared/Index.ascx
~/Views/ContentCategory/Index.aspx
~/Views/ContentCategory/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Areas/CMS/Views/ContentCategory/Index.cshtml
~/Areas/CMS/Views/ContentCategory/Index.vbhtml
~/Areas/CMS/Views/Shared/Index.cshtml
~/Areas/CMS/Views/Shared/Index.vbhtml
~/Views/ContentCategory/Index.cshtml
~/Views/ContentCategory/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
但是 ~/Areas/CMS/Views/ContentCategory/Index.cshtml
存在于我的主机上!
2) 其他一些请求,如 mysite.com/cms/content
或 mysite.com/cms/gallery
有此错误:
The partial view '~/Content/templates///views/Gallery/index.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
~/Content/templates///views/Gallery/index.cshtml
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The partial view '~/Content/templates///views/Gallery/index.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
~/Content/templates///views/Gallery/index.cshtml
Source Error:
Line 3: string view = "~/Content/templates/" + ViewBag.website + "/" + ViewBag.lang + "/views/Gallery/index.cshtml";
Line 4: Html.RenderPartial(view);
Line 5: }
Line 6:
这个错误的 'Source Error' 显示了 galleryController 的默认项目(不是 cms)视图的一些代码!
我很困惑。
我再次强调这只发生在主机上,在我的本地系统上一切都是正确的!
还应该注意的是,今天这个错误发生在另一个错误之后,昨天在我的主机上所有的东西都被更正了,这个错误直到昨天才出现!
我不知道这是否是您唯一的问题,但是您列出的路线顺序错误。
根据您配置路由的方式,任何以 CMS
开头的 2 或 3 段 URL 将匹配 Default
路由,而不是 CMS
路由。由于这个问题,可能因为使用了错误的命名空间(因此调用了错误的控制器)而无法找到您的视图。要解决此问题,您应该在 Default
.
之前注册 CMS
路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"CMS",
"CMS/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "CMS.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Peacock.Controllers" }
);
}
当然,该错误也可能表明您尚未将所有视图完全部署到服务器。
我也有同样的问题!我错误地将我的 'Areas' 文件夹重命名为 'Area',并遇到了这个错误!
错误 2:当您在默认项目中有一个与请求的控制器同名的控制器时会发生!
错误 1:当您在默认项目中没有与请求的控制器同名的控制器时发生!
祝你好运。
我正在使用带区域的路线属性。
我的路线配置是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Peacock.Controllers" }
);
routes.MapRoute(
"CMS",
"CMS/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "CMS.Controllers" }
);
}
在我的本地,一切都是正确的!今天,当我发布我的项目并将其上传到我的主机时,我遇到了两种类型的错误。
如果我请求 url 的默认 MapRoute,例如 mysite.com/Contents/1060
,一切都是正确的!但是当我请求我所在区域的 url 时,我遇到了两种类型的错误!
1) 某些请求如 mysite.com/cms/comment
或 mysite.com/cms/category
有此错误:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/CMS/Views/ContentCategory/Index.aspx
~/Areas/CMS/Views/ContentCategory/Index.ascx
~/Areas/CMS/Views/Shared/Index.aspx
~/Areas/CMS/Views/Shared/Index.ascx
~/Views/ContentCategory/Index.aspx
~/Views/ContentCategory/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Areas/CMS/Views/ContentCategory/Index.cshtml
~/Areas/CMS/Views/ContentCategory/Index.vbhtml
~/Areas/CMS/Views/Shared/Index.cshtml
~/Areas/CMS/Views/Shared/Index.vbhtml
~/Views/ContentCategory/Index.cshtml
~/Views/ContentCategory/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
但是 ~/Areas/CMS/Views/ContentCategory/Index.cshtml
存在于我的主机上!
2) 其他一些请求,如 mysite.com/cms/content
或 mysite.com/cms/gallery
有此错误:
The partial view '~/Content/templates///views/Gallery/index.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
~/Content/templates///views/Gallery/index.cshtml
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The partial view '~/Content/templates///views/Gallery/index.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
~/Content/templates///views/Gallery/index.cshtml
Source Error:
Line 3: string view = "~/Content/templates/" + ViewBag.website + "/" + ViewBag.lang + "/views/Gallery/index.cshtml";
Line 4: Html.RenderPartial(view);
Line 5: }
Line 6:
这个错误的 'Source Error' 显示了 galleryController 的默认项目(不是 cms)视图的一些代码! 我很困惑。
我再次强调这只发生在主机上,在我的本地系统上一切都是正确的!
还应该注意的是,今天这个错误发生在另一个错误之后,昨天在我的主机上所有的东西都被更正了,这个错误直到昨天才出现!
我不知道这是否是您唯一的问题,但是您列出的路线顺序错误。
根据您配置路由的方式,任何以 CMS
开头的 2 或 3 段 URL 将匹配 Default
路由,而不是 CMS
路由。由于这个问题,可能因为使用了错误的命名空间(因此调用了错误的控制器)而无法找到您的视图。要解决此问题,您应该在 Default
.
CMS
路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
"CMS",
"CMS/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "CMS.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Peacock.Controllers" }
);
}
当然,该错误也可能表明您尚未将所有视图完全部署到服务器。
我也有同样的问题!我错误地将我的 'Areas' 文件夹重命名为 'Area',并遇到了这个错误!
错误 2:当您在默认项目中有一个与请求的控制器同名的控制器时会发生!
错误 1:当您在默认项目中没有与请求的控制器同名的控制器时发生!
祝你好运。