[英]MVC 3 Area route not working
我在我的MVC 3應用程序中創建了一個名為“Blog”的區域。
在global.asax中,我有以下代碼。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
這是我所在地區的代碼
public class BlogAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Blog"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Blog_default",
"Blog/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
當我轉到以下URL http:// localhost / CMS / blog時,我收到以下錯誤。
未找到視圖“索引”或其主數據或視圖引擎不支持搜索的位置。 搜索了以下位置:〜/ Views / blog / Index.aspx~ / Views / blog / Index.ascx~ / Views / Shared / Index.aspx~ / Views / Shared / Index.ascx~ / Views / blog / Index。 cshtml~ / Views / blog / Index.vbhtml~ / Views / Shared / Index.cshtml~ / Views / Shared / Index.vbhtml
我該如何解決這個問題?
我找到了我認為是框架中的錯誤,並找到了解決方法。 如果您嘗試將默認路由映射到帶有區域的MVC 3應用程序,則global.asax文件可能具有以下內容:
VB:
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
New With {.area = "MyArea", .controller = "Home", .action = "Index", .id = UrlParameter.Optional}
)
C#:
routes.MapRoute(
"Default",
"{area}/{controller}/{action}/{id}",
new { area = "MyArea", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
如果您轉到URL中的應用程序根目錄,則可能會出現如下運行時錯誤:
未找到視圖“索引”或其主數據或視圖引擎不支持搜索的位置。 搜索了以下位置:
出於某種原因,視圖引擎似乎不會在視圖文件的區域文件夾中查找與鍵入整個鏈接相同的內容。 奇怪的是代碼到達控制器動作。 以下是修復:將此代碼放在您的控制器操作中:
VB:
If Not Me.ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
Me.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
End If
C#
if (!this.ControllerContext.RouteData.DataTokens.ContainsKey("area"))
{
this.ControllerContext.RouteData.DataTokens.Add("area", "MyArea")
}
您所在地區的注冊似乎是錯誤的。 您為操作指定了默認值,但沒有為控制器指定默認值。 由於您通常將Home作為控制器的名稱,因此您需要指定它。
也可能是您沒有正確設置文件夾,因為您應該進行物理設置:
...一旦您修復了博客區域路線,您還需要:
你得到的錯誤似乎很清楚地表明這是問題所在。
放松! 這可以節省您的閱讀時間,制作咖啡並觀看這個3分鍾的視頻,一切都將清晰。 http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas (我相信它也適用於mvc3,mvc4和mvc2035)
我正在使用Phil Haack的routedebugger來解決這個問題。 它可以方便地顯示所有已注冊的路由以及輸入的URL如何與您的配置匹
它通過覆蓋常規應用程序流來工作,您可以通過在Application_Start末尾添加此行來啟用它:
RouteDebug.RouteDebugger.RewriteRoutesForTesting( RouteTable.Routes );
繼Mortens回答之后 ,您現在可以NuGet(或手動下載並安裝)提供此功能的RouteMagic或Glimpse軟件包等等。
Phil Haacks博客提供了有關其工具狀態及其變形的更多信息。 這些評論也有助於閱讀!
檢查是否將虛擬路徑映射為區域名稱。 我在visual studio中設置區域地址只是為了調試,它讓我創建一個虛擬路徑。 所以AppRelativeCurrentExecutionFilePath總是〜/並且路由無法確定區域。 對於IIS express,刪除站點的虛擬路徑:
C:\Users\username\Documents\IISExpress\config\applicationhost.config
我花了4天時間才發現它。
我遇到了與Mindstorm Interactive相同的問題,采取了略微不同的方法。 是的,它確實重定向了用戶,但它使我的代碼看起來不那么糟糕。
我創建了一個新的控制器,無論在哪個區域,因為問題是在渲染器內找不到視圖,而不是控制器。
然后控制器重定向包含的區域(如在Mindstroms修復,鍵中),並且瞧。
public class StartController : Controller
{
public ActionResult Index()
{
return RedirectToAction("Index", "MyController", new { area = "MyArea" });
}
}
在RouteConfig中添加
routes.MapRoute(name: "Root", url: "", defaults: new { controller = "Start", action = "Index", area = "MyArea" });
希望它可以幫助某人。
只是嘗試刪除此處提到的以下目錄中的內容並重建項目
C:\\ Temp C:\\ Users \\%用戶名%\\ AppData \\ Local \\ Microsoft \\ VisualStudio C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ Temporary ASP.NET Files C:\\ Windows \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ Temporary ASP.NET Files Path \\ To \\ Your \\ Project \\ obj \\ Debug
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.