ASP.NET MVC默认URL视图

Oun*_*ess 14 asp.net asp.net-mvc web-config url-routing asp.net-mvc-routing

我正在尝试将我的MVC应用程序的默认URL设置为我的应用程序区域内的视图.该区域称为" Common ",控制器为" Home ",视图为" Index ".

我已经尝试将web.config的表单部分中的defaultUrl设置为" 〜/ Common/Home/Index "但没有成功.

我也尝试在global.asax中映射一个新路由,因此:

routes.MapRoute(
        "Area",
        "{area}/{controller}/{action}/{id}",
        new { area = "Common", controller = "Home", action = "Index", id = "" }
    );
Run Code Online (Sandbox Code Playgroud)

再一次,无济于事.

Geo*_*ker 13

您列出的路线仅在明确键入URL时才有效:

yoursite.com/{area}/{controller}/{action}/{id}
Run Code Online (Sandbox Code Playgroud)

这条路线说的是:

如果我收到一个请求,该请求在该区域中有效{area},有效{controller},并且{action}该控制器中有效,则将其路由到那里.

如果他们只是访问您的网站,您想要的是默认为该控制器yoursite.com:

routes.MapRoute(
    "Area",
    "",
    new { area = "Common", controller = "Home", action = "Index" }
);
Run Code Online (Sandbox Code Playgroud)

这说的是,如果他们没有附加任何东西,http://yoursite.com那么将其路由到以下操作:Common/Home/Index

另外,将它放在路由表的顶部.

确保您还让MVC知道您在应用程序中注册的区域:

将以下Application_Start方法放在Global.asax.cs文件中的方法中:

AreaRegistration.RegisterAllAreas();
Run Code Online (Sandbox Code Playgroud)


use*_*993 7

你要做的是:

  • 从global.asax.cs中删除默认路由

    //// default route map will be create under area
    //routes.MapRoute(
    //    name: "Default",
    //    url: "{controller}/{action}/{id}",
    //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    //);
    
    Run Code Online (Sandbox Code Playgroud)
  • 更新区域Common中的SecurityAreaRegistration.cs

  • 添加以下路由映射:

     context.MapRoute(
        "Default",
        "",
        new { controller = "Home", action = "Index", id = "" }
    );
    
    Run Code Online (Sandbox Code Playgroud)