Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Default URL View

I'm trying to set the Default URL of my MVC application to a view within an area of my application. The area is called "Common", the controller "Home" and the view "Index".

I've tried setting the defaultUrl in the forms section of web.config to "~/Common/Home/Index" with no success.

I've also tried mapping a new route in global.asax, thus:

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

Again, to no avail.

like image 787
Oundless Avatar asked Jan 05 '10 14:01

Oundless


People also ask

How do I change the default URL in MVC?

You can set up a default route: routes. MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults ); Just change the Controller/Action names to your desired default.

What is the default route in MVC?

The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home.

How are URL patterns mapped to a handler in ASP.NET MVC?

Typical URL Patterns in MVC Applications URL patterns for routes in MVC Applications typically include {controller} and {action} placeholders. When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler.

What is RouteConfig Cs in ASP.NET MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.


1 Answers

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();
like image 119
George Stocker Avatar answered Sep 24 '22 18:09

George Stocker