ASP.NET MVC Default URL View

◇◆丶佛笑我妖孽 提交于 2019-11-29 01:32:12
George Stocker

The route you listed only works if they explicitly type out the URL:

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

What that route says is:

If I get a request that has a valid {area}, a valid {controller} in that area, and a valid {action} in that controller, then route it there.

What you want is to default to that controller if they just visit your site, yoursite.com:

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

What this says is that if they don't append anything to http://yoursite.com then to route it to the following action: Common/Home/Index

Also, put it at the top of your routes table.

Makes sure you're also letting MVC know to register the areas you have in the application:

Put the following in your Application_Start method in the Global.asax.cs file:

AreaRegistration.RegisterAllAreas();
user279993

What you have to do is:

  • Remove Default Route from 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 }
    //);
    
  • Update SecurityAreaRegistration.cs in area Common

  • Add following route mapping:

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

What you're doing seems correct. If I had to guess I would say this is happening due to the way you are running your website. In Visual Studio, if you have a specific view selected when you hit F5 that view will be the starting Url - try selecting the Project and then hitting F5?

Egli Becerra

In the Global.asax delete the .MapRoute

and make it look like

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

then under the area's yourareaAreaRegistration.cs (its there if you added the area through 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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!