在MVC項目中經常會使用到Area來分開不同的模塊讓項目結構更加的清晰。如果想網站打開默打開Area下的控制器時會出現以下的錯誤
“/”應用程序中的服務器錯誤。
未找到視圖“Index”或其母版視圖,或沒有視圖引擎支持搜索的位置。搜索了以下位置:
~/Views/Default/Index.aspx
~/Views/Default/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Default/Index.cshtml
~/Views/Default/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
解決方法:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional}, namespaces:new string[]{"MVCUI.Areas.自己Area的名稱.Controllers"} ).DataTokens.Add("Area","自己Area的名稱"); }
超鏈接使用法:
return RedirectToAction("action", "controller", new { area = "area-name" });
<%= Html.ActionLink("超鏈接內容", "action", "controller", new { area = "area-name" }, null) %>