Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default controller in MVC4

How to set the default controller in MVC4?

I've tried adding the below code to the Global.ascx but it said "Only assignment, call, increment, decrement, and new object expressions can be used as a statement", seems it can't find the "route", did I put it on the wrong place?

routes.MapRoute(
        "Default", 
        "{controller}/{action}/{id}", 
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
);

Below is the screenshot: enter image description here

like image 632
User2012384 Avatar asked Dec 21 '25 09:12

User2012384


2 Answers

Take a look at App_Start/RouteConfig.cs file. This is where you will be able to configure routes in the way you want.

There, you will find code similar to this:

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
like image 71
Aleksandar Vucetic Avatar answered Dec 22 '25 22:12

Aleksandar Vucetic


you need to put this code inside RouteConfig.cs under App_Start.

check ASP.NET MVC 4: Where Have All The Global.asax Routes Gone?

like image 37
Damith Avatar answered Dec 22 '25 22:12

Damith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!