首页 文章

ASP.NET MVC默认URL视图

提问于
浏览
14

我正在尝试将我的MVC应用程序的默认URL设置为我的应用程序区域内的视图 . 该区域被称为“ Common ", the controller " Home " and the view " Index ” .

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

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

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

再一次,无济于事 .

4 回答

  • 7

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

    yoursite.com/{area}/{controller}/{action}/{id}
    

    这条路线说的是:

    如果我收到的请求具有有效的 {area} ,该区域中的有效 {controller} 以及该控制器中的有效 {action} ,则将其路由到那里 .

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

    routes.MapRoute(
        "Area",
        "",
        new { area = "Common", controller = "Home", action = "Index" }
    );
    

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

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

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

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

    AreaRegistration.RegisterAllAreas();
    
  • 13

    你要做的是:

    • 从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 }
    //);
    
    • 更新CommonAreaRegistration.cs区域Common

    • 添加以下路线映射:

    context.MapRoute(
        "Default",
        "",
        new { controller = "Home", action = "Index", id = "" }
    );
    
  • 0

    你在做什么似乎是正确的 . 如果我不得不猜测,由于您运行网站的方式,我会说这种情况正在发生 . 在Visual Studio中,如果在按F5时选择了特定视图,则该视图将成为起始URL - 尝试选择项目然后点击F5?

  • 0

    在Global.asax中删除.MapRoute

    并使它看起来像

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

    然后在该区域的yourareaAreaRegistration.cs(如果你通过VS添加区域那里)

    public override void RegisterArea(AreaRegistrationContext context)
    {
         //This is the key one         
         context.MapRoute("Root", "", new { controller = "Home", action = "Login" });
    
         //Add more MapRoutes if needed
    }
    

相关问题