標籤:style blog io color ar 使用 sp div on
在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) %>
mvc 預設訪問 Area 下控制器方法