Microsoft has been launching quite a few latest software as well as programs, among which MVC is one. MVC has a lot of unique and special features; among them the routing function is been touted as the best improvement, especially since it has become attribute based. Initially the function of MVC routing was for cleaning of URL semantics of sites that have been built on it. However, now the function as well as importance have changed dramatically, making the entire concept cleverer and more useful.
Many of us have doubts regarding the need of routing. Features like Controllers, Models and Views are deemed as important parts of the application since any application needs user interaction which is handled by controllers. Controller also selects appropriate views to be rendered. The UI or View is used by users when interacting with applications; while the data required for an application is represented by Model. But when we come to routing, we do not see it addressing any concerns like those mentioned.
Traditionally in web applications, a user enters URL in browser maps for location of particular physical file that is mapped to web server’s directory which is often called virtual directory. For example when we are using an URL, it refers to products or businesses in question. When you look at the URL you will be able to judge the line of business or category; it is easily discernible as it lists products or services of that category. After displaying that fact subcategories must be added which will display only things related to that topic. For the same, a filter has to be applied on the page so that the user clicking on the hyperlink, gets a unique Id representing that object. This solves the problem of directing him or her to particular categories. Now the user will be able to pinpoint and look at those things that he or she is interested in.
To lend a unique ID if we use numbers there are chances that the user will simply forget the numbers but adding words will be better than numbers as it will be easier to remember. Adding words to URL names them as hackable URL and it is here that routing gives benefits. Routing is very important here as there are no physical files in MVC for better handling of different requests, contrary traditional applications and this routing mechanism helps MVC decide which controller class’s action method to be used for execution. There can be no mapping of action method to requests without routing. This MVC architecture’s part- routing is supported by default.
Routing enables us to create URLs which are search engine optimized and that too very easily. This is because when using this there are no restrictions regarding fixed URLs which can be easily re-factored so that search engines can discover them easily. Custom routes can be created as well.
Attribute-based routing gives you finer control on the routes of both action and controller levels. Enabling this is a simple task; just take your RegisterRoutes function and add call route .MapMvcAttributes(). You can also add attributes at controller level and this will apply to every action within controller unless you have added some particular route to some action.
You also have the option of doing routing on actions based on constraints. This is quite akin to function overrides as matching of a route will occur if there is matching of data type; if not the request will be transferred to another route which matches the pattern for some supported data type. Multiple constraints can be applied to single route so that precise control can be gained over URLs, routes and actions. You can also use for convention and attribute based ones simultaneously.
You will be able to free URLs from being physically associated with files by implementing this, allowing for plenty of flexibility regarding the interpretation and presentation of URLs in MVC applications. This would help you leverage the benefits of .net web development.