mvc 默認訪問 Area 下控制器方法


在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) %>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM