Controllers In MVC By Sagar Jaybhay

In this article you will understand what Is Controllers In MVC and What is the use of Controller in Asp.Net MVC.

Controllers In MVC

In MVC the URLs are mapped to the Controllers action method so the question is how URLs are mapped to the Controllers action method? The answer for that ASP.NET routing. It is registering in Global.asax file.

Gloabl.asax File In MVC
Gloabl.asax File In MVC

The Registeroute method Is present in RouteConfig.cs file see below image,

RegisterRoute Method In MVC
RegisterRoute Method In MVC

Below is the RegisterRoute method in that class file.

public static void RegisterRoutes(RouteCollection routes)

                name: "Default",
                url: "controller/action/id",
                defaults: new  controller = "Home", action = "Index", id = UrlParameter.Optional 

In the above code, you can see, the Default route is present it means if you simply run the application then it will invoke this URL path the normal path which you see after run application https://localhost:44374/.

This URL is the same as https://localhost:44374/Home/Index this URL is the same as the above URL. The controller is Home and action is Index.

So in the RegisterRoute method MapRoute method if see id is an optional parameter but if you pass and you need to access this you need to modify action method signature in controller and code looks like this below.

QueryString In MVC
QueryString In MVC

This is one way to get QueryString parameter from URL to action but you can also use our older way to access the parameters. Use the below method for that.

public ActionResult Index(string id)
   = id;
            return View();

Above id code for that, in above code we use Request.QuerySting method to access URL parameters and application works as expected.

QueryString In MVC 1
QueryString In MVC 1

In Register.Route method one more method


Which is used to ignore route path for the file whose extension .axd. It means that the request is not passed to the below MapRoute method and file directly get from this.

GitHub –

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s