Routing is that the method of leading associate protocol request to a controller and therefore the practicality of this process is enforced in System.Web.Routing. This assembly isn’t a part of ASP.NET MVC. it’s really a part of the ASP.NET runtime, and it had been formally free with the ASP.NET as a .NET 3.5 SP1.
System.Web.Routing is employed by the MVC framework, however it is also utilized by ASP.NET Dynamic knowledge. The MVC framework leverages routing to direct letter of invitation to a controller. The Global.asax file is that a part of your application, wherever you’ll outline the route for your application.
This is the code from the appliance begin event in world.asax from the MVC App that we tend to created within the previous chapter.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCRouting {
 public class MvcApplication : System.Web.HttpApplication {
 protected void Application_Start(){
 AreaRegistration.RegisterAllAreas();
 RouteConfig.RegisterRoutes(RouteTable.Routes);
 }
 }
}

Following is that the implementation of RouteConfig category, that contains one technique RegisterRoutes.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCRouting {
 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});
 }
 }
}

You can outline the routes and people routes will map URLs to a selected controller action. associate action is simply a way on the controller. It can even decide parameters out of that uniform resource locator and pass them as parameters into the strategy.
So this route that’s outlined within the application is that the default route. As seen within the on top of code, after you see a uniform resource locator arrive within the sort of (something)/(something)/(something), then the primary piece is that the controller name, second piece is that the action name, and therefore the third piece is associate ID parameter.

Understanding Routes

MVC applications use the ASP.NET routing system, that decides however URLs map to controllers and actions.
When Visual Studio creates the MVC project, it adds some default routes to urge North American country started. after you run your application, you’ll see that Visual Studio has directed the browser to port 63664. you’ll virtually definitely see a unique port range within the uniform resource locator that your browser requests as a result of Visual Studio allocates a random port once the project is made.

In the last example, we’ve got else a AboutController, thus you’ll conjointly request any of the subsequent URLs, and that they are going to be directed to the Index action on the AboutController.
http://localhost:63664/About/
http://localhost:63664/About/Index
When a browser requests http://abcsite/ or http://abcsite/Home, it gets back the output from AboutController’s Index technique.
You can do that yet by ever-changing the uniform resource locator within the browser. during this example, it’s

http://localhost:63664/, except that the port can be totally different.
If you append /About or /About/Index to the uniform resource locator and press ‘Enter’ button, you’ll see constant result from the MVC application.

As you’ll see during this case, the convention is that we’ve got a controller referred to as AboutController and this AboutController are going to be the start line for our MVC application.
The default routes that Visual Studio creates for a replacement project assumes that you simply can follow this convention. however if you would like to follow your own convention then you’d have to be compelled to modify the routes.

Custom Convention

You can definitely add your own routes. If you do not like these action names, if you’ve got totally different ID parameters or if you simply generally have a unique uniform resource locator structure for your web site, then you’ll add your own route entries.
Let’s take a glance at a straightforward example. contemplate we’ve got a page that contains the list of Employee. Following is that the code, which is able to route to the method page.

routes.MapRoute(
 "Employee",
 "Employee/{action}/{id}",
 defaults: new{
 controller = "Employee", action = "List ", id = UrlParameter.Optional}
);

When somebody comes in and appears for a uniform resource locator with Employee/Action/Id, they’re going to move to the method Controller. we are able to create the action a trifle bit totally different, the default action, we are able to create that a listing rather than Index.
Now letter of invitation that arrives feels like localhosts/Employee. The routing engine can use this routing configuration to pass that on, thus it’s progressing to use a default action of List.
Following is that the complete category implementation.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCRouting{
 public class RouteConfig{
 public static void RegisterRoutes(RouteCollection routes){
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
 routes.MapRoute(
 "Employee", "Employee/{action}/{id}",
 defaults: new{
 controller = " Employee", action = "List ", id =
 UrlParameter.Optional});
 
 routes.MapRoute(
 name: "Default", url: "{controller}/{action}/{id}",
 defaults: new{
 controller = "Home", action = "Index", id =
 UrlParameter.Optional});
 }
 }
}

Step one − Run this and request for a method page with the subsequent uniform resource locator http://localhost:63664/Employee

You will see associate protocol 404, as a result of the routing engine is yearning for EmployeeController, that isn’t out there.
Step two− Produce EmployeeController by right-clicking on Controllers folder within the answer someone and choose Add → Controller.

It will show the Add Scaffold dialog.

Step three − choose the MVC five Controller – Empty choice and click on ‘Add’ button.
The Add Controller dialog can seem.

Step four − Set the name to EmployeeController and click on ‘Add’ button.
Now you’ll see a replacement C# file EmployeeController.cs within the Controllers folder, that is open for piece of writing in Visual Studio yet.

Now our default action goes to be List, thus we wish to own a listing action here rather than Index.
Step five − amendment the come back sort from ActionResult to string and conjointly come back some string from this action technique exploitation the subsequent code.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCRouting.Controllers{
 public class EmployeeController : Controller{
 // GET: Employee
 public string List(){
 return "This is Employee page";
 }
 }
}

Step Six − after you run this application, once more you’ll see the result from the default route. after you specify the subsequent uniform resource locator, http://localhost:63664/Employee/List, then you’ll see the result from the EmployeeController.

11 Responses on “Routing in mvc.

  1. I do not know if it’s just me or if perhaps everybody else experiencing problems with your website. It appears as if some of the text on your posts are running off the screen. Can somebody else please comment and let me know if this is happening to them as well? This may be a problem with my web browser because I’ve had this happen before. Thank you
    http://educationclue.eu

  2. Wonderful work! This is the kind of info that are meant to be shared around the net. Disgrace on the search engines for not positioning this submit higher! Come on over and visit my web site . Thank you =)

  3. My spouse and I absolutely love your blog and
    find most oof your post’s to be just what I’m lookoing for.
    cann you offer guest writers to write content
    forr you personally? I wouldn’t mind producing a post or elaborating on most of
    the subjects yoou write oncerning here. Again, awesome site!

  4. I do accept as true with all thhe ideas you have offered in your post.
    They’re really convincing and will definitely work.
    Nonetheless, the posts are too quick for beginners. May just
    youu please extend them a bit from next time? Thanmk you for the post.

Leave a Reply

Your email address will not be published. Required fields are marked *