articles

Home / DeveloperSection / Articles / Routers and Controller in ASP.NET MVC 3

Routers and Controller in ASP.NET MVC 3

Routers and Controller in ASP.NET MVC 3

Vijay Shukla 11406 06-Dec-2012

Routing:

In this article, we look at how routers work and how you can use them in your MVC web application. MVC gives you huge control over how URLs are mapped to your controllers. Routing is a procedure to talking about the Uniform Resource Identifier end point and decomposing it into parameters to decide which module, controller, and action of that controller should accept the request and one of the main goals for ASP.NET MVC3 is Search Engine Optimization because Search Engine is work on URLs, and through the Routers we can make more significant andconceivable URLs for the Search Engine. ASP.NET MVC3 application is don’t represent the extension as like .aspx in its place, the URLs consist of the Controller name and Action name. It’s hiding what kind of page a user is calling and what environment we are working in.

Understanding the default Routes declaration

When we create any kind of MVC application by default Global.asax file is created because ASP.NET implements MVC using this global application class mainly. Routes defined in the Global.asax.cs file in MVC web application. In this global.asax file most important part relative to our work is RegisterRoutes method. By default, there is only one route defined in the RegisterRoutes method that looks like the line below.


public static void RegisterRoutes(RouteCollection routes)

{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(
   name: "Default", // Route name
   url: "{controller}/{action}/{id}",// URL with parameters
   defaults: new
   {
    controller = "Home", action = "Index", id = UrlParameter.Optional//Parameter defaults
   }
  );
}


This route is defines the route name and if You can add other routes by copying the same line and adjust the URL parameters and the associated default values, and keep in mind when we add other routes the order is important. And if you want to make any other default route in your ASP.NET MVC3 application.  if you have a Account Controller with Login action and you want to make a default Route then you can perform the changes in the RegisterRoutes method:


public static void RegisterRoutes(RouteCollection routes)

{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(
   name: "Default", // Route name
   url: "{controller}/{action}/{id}",// URL with parameters
   defaults: new
   {
    controller = "Account", action = "Login", id = UrlParameter.Optional//Parameter defaults
   }
  );
}
Controller

The controllers will interaction with the user request. Controller watch out the whole execution of request. It’s got the request from the web server and checks the data necessities.

Now let us create a new Controller and routed to with this new Controller using a different routing pattern in our ASP.NET MVC3 Application. Add a new controller with the Account name after adding controller we can see that  this controller with have a default action name Index. Now it’s renamed with Login.

public ActionResult Login()

{
   return View();
}


As there are no Views corresponding to AccountController, let us return some text from our Action. For returning any text data from an Action, use the Content class.


public ActionResult Login()

{
   return Content (“Welcome for Login Page”);
}

 

Now run the application with “/Account/LoginURL.


Routers and Controller in ASP.NET MVC 3


But if we can give only controller name as “/Account” and don’t specify the action name, now we can get the 404(Resource not fount found)  error. if we talking about the routing term, here is  no defining the action  name so it should redirect to the Index action in the specified Controller.


Routers and Controller in ASP.NET MVC 3


Now Resolve this problem you can Add a new Route in RegisterRoutes method with AccountRoute” Name.


routes.MapRoute(

                "AccountRoute", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new
                {
                    controller = "Account",
                    action = "Login",
                    id = UrlParameter.Optional
                } // Parameter defaults
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new
                {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                } // Parameter defaults
            );

 

After Adding new Route now you can Run your application and see that output:


Routers and Controller in ASP.NET MVC 3

Pass the Data via URL:

We can pass the Data via URL and show in the View with the some changes Route and controller these are given below:

Changes in MapRoute method:

routes.MapRoute(

                "AccountRoute", // Route name
                "{controller}/{userName}", // URL with parameters
                new
                {
                    controller = "Account",
                    action = "Login",
                } // Parameter defaults
            );

 

Changes in Login Action:

public ActionResult Login(string userName)

{
     return Content(userName);
}

 

After these changes you can Run your application and see the below screen:


Routers and Controller in ASP.NET MVC 3

Conclusion

In this article we saw the main advantage of url routing of MVC. Using this description I think so you are clear about Routing can perform the routing in your MVC application with the different controller.


Updated 14-Feb-2020

Leave Comment

Comments

Liked By