MVC和WebApi中設置Area中的頁為首頁


拿WebApi為例,我們一般會生成一份幫助文檔,幫助文檔會在Area中

我們現在要講幫助文檔設為首頁

只需在App_Start文件夾下添加 RouteConfig 類

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Help", action = "Index", id = UrlParameter.Optional }
            ).DataTokens.Add("Area", "HelpPage");
        }
    }

增加 DataTokens.Add("Area", "HelpPage");

因為幫助文檔會生成在HelpPage的Area中  而幫助文檔的首頁是 /Help/Index

在 Global.asax 的 Application_Start 中注冊剛添加的路由

        protected void Application_Start()
        {
            //注冊域,這里注冊時因為幫助文檔放在域中
            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

 

 


免責聲明!

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



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