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