13

我想我的应用程序的一个区域内,我的MVC应用程序的默认URL设置为一个视图。该地区被称为“常见”控制器“”和视图“指数”。ASP.NET MVC默认网址查看

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

我还试图映射在Global.asax的新路线,即:

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

再次,无济于事。

+1

在进一步的调查(如果你通过添加VS该地区的存在),看来要求正在被引导到正确的控制器(即MyApp.Areas.Common.Controllers.HomeController)有或没有您建议的更改。但是,在这两种情况下,ViewEngine只搜索〜/ Views/Home和〜/ Views/Shared文件夹,而不是以〜/ Areas/Common/Views/Home和〜/ Areas/Common/Views/Shared开头。奇怪的是,如果我使用ActionLink创建一个页面到相同的控制器方法(即Index()),那么它工作正常。嗯。 – 2010-01-06 10:41:27

+0

http://stackoverflow.com/questions/2140208/how-to-set-a-default-route-to-an-area-in-mvc这可能会有所帮助。我有类似的问题。 – LiamB 2010-02-11 08:31:29

回答

12

你只列出的路线工作,如果他们明确地输入了网址:

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知道注册您的应用程序有几个方面:

把你Application_Start方法如下在Global.asax.cs文件:

AreaRegistration.RegisterAllAreas(); 
+1

对我来说,这是在根文件上搜索,而不是在该地区。 – FrenkyB 2016-06-29 10:44:14

0

你是什么做得似乎正确。如果我猜我会说这是发生因您运行的网站的方式。在Visual Studio中,如果您在按F5时选择了特定的视图,那么该视图将成为起始URL - 尝试选择Project,然后按F5?

+0

恐怕这不是问题的原因。尽管感谢您的建议。 – 2010-01-06 10:22:50

+0

这是值得一试的:D – 2010-01-06 10:43:55

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 } 
    //); 
    
  • 更新SecurityAreaRegistration.cs映射:

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

在Global.asax 删除。图路线

,并使它看起来像

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

然后在该地区的yourareaAreaRegistration.cs

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

    //Add more MapRoutes if needed 
}