1

我正在使用具有区域的路由属性。当在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" } 
     ); 
} 

在我的本地每一个东西是正确的!今天,当我发布我的项目并将其上传到我的主机上时,我遇到了两种类型的错误。

如果我要求默认MapRoute的url,如mysite.com/Contents/1060一切都是正确的!但是当我要求我的地区的网址时,我遇到了两类错误!

1),如mysite.com/cms/commentmysite.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/contentmysite.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: 

这个错误的“源错误”显示我的默认项目galleryController的(不是CMS)鉴于一些代码! 我很困惑。

我再次强调这只是在主机和我的本地系统上发生的每件事情都是正确的!

还应该指出,这个错误发生在今天又一个错误之后,昨天在我的主机上一切都被纠正了,这个错误直到昨天才发生!

+0

您可以显示您从/ cms/comment方法返回的视图吗? – Shyju

+0

返回这个视图'〜/ Areas/CMS/Views/Comment/Index.cshtml' for/cms/comment但是对于/ cms/category有相同的错误 – Mostafa

+0

为什么你在'RouteConfig.cs'中写了'Area'路由?你的CMS区域应该有一个名为'CMSAreaRegistration.cs'的文件。 – Azim

回答

1

我有同样的问题!我错误地将'Areas'文件夹重命名为'Area',并面临这个错误!

错误2:当您在Default项目中拥有与请求的控制器同名的控制器时发生!

错误1:当您在Default项目中没有与请求的控制器同名的控制器时发生!

祝你好运。

0

我不知道这是否是您唯一的问题,但您的路线以错误的顺序列出。

配置路由的方式是,任何以CMS开头的2或3段网址将匹配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" } 
    ); 
} 

当然,错误也可能表明您没有完全将所有视图部署到您的服务器。

+0

我修正了你的建议,但错误仍在继续! – Mostafa