Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This tutorial will help you to get started with ASP.NET MVC

Getting started with ASP.NET MVC

Getting Started with ASP.NET MVC - Part 9: URL Routing in ASP.NET MVC

Jun 12 2012 12:00AM by Jalpesh   

In previous chapter, we have learnt about how we can create CRUD application. In this chapter, we will learn how URL routing works in ASP.NET MVC.

Why we need URL Routing:

For any web application, Search Engine Optimization is very much important as your proposed user may come from this search engine. URL routing helps us to rewrite URL in such a way that can be more understandable by a Search Engine. Now let us see how we can help search engine to crawl our site in proper way via search engine friendly URLs.

Let us consider a scenario where we need to display details about a particular user. However, how our page or view will know that for which user we need to display details. Simplest way to doing this is to pass query string in URL. Therefore, in earlier versions of ASP.NET we were passing the querystring in the URL as below:

Users.aspx?Id=1

Technically, this will work but this URL is not user friendly as well as search engine friendly. Here another problem is that we need to pass a query string parameter. So, every time when we redirect a page from another URL at that time we have to remember that we need to pass query string parameter. In addition, when search engine will crawl this site, at that time, it will not be able to detect the purpose of particular URL and it will crawl it blindly.

But with ASP.NET MVC, we already have such feature called ‘URL Routing’ which will allow us to write our URLs in such a way that it is more user friendly or search engine friendly like following.

User/Details/1

Looking at above URL, anybody can understand that we are talking about user details and this page is going to show user details.

How URL routing will work in ASP.NET MVC

In ASP.NET MVC, URL routing is important mapping browser request to particular MVC controller actions.

Once you create an ASP.NET MVC application in visual studio and examine your global.asax file, you will find the code for the URL routing. You will find following code over there.

   public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }

      }

Here in the above code you can see that there is application_start method, which will be called when ASP.NET MVC application will start. This method will execute a RegisterRoutes method. This register routes method will call route table and our asp.net MVC application will rewrite URL according to it. In the code RegisterRoutes method, you can see that there were two things written.

First statement is routes.IgnoreRoute, which asks URL routing mechanism to ignore the .asxp files.

Second statement is the main part where whole URL routing will work. In this routes.MapRoute, the first parameter is URL Route Name. Here, you can put any valid string. Another parameter is URL with parameter and third parameter is for Parameter default values will be passed.

In above statement, default is Route Name and it will contain any valid string. Second parameter is the URL parameter for controllers/Action/Id where controller can be any valid string for controller name, Action will be redirect back to Action Method and Id parameters are optional to pass. It depends on the situation.

Here in the above code, we have passed the default values to all the route. So, in case of no values provided, it will directly put that default value.

So our will be as follows:

/Home

/Home/1

This will map to following for code for index action method for Home controller.

public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

So, when you URL like /Home, it will call this method and generate response in terms of HTML.

Same way it will work fine our example of earlier chapter. Let us take edit example.

User/Edit/1 it will call following method and render view.

public ActionResult Edit(int id)
        {
            User user = db.Users.Single(u => u.UserId == id);
            return View(user);
        }

And it will look like following in browser.

User/Edit/1- URL will put particular user details like the following:

edit

In addition, if you change the URL for the like User/Edit/2 It will display the following information:

edit 2

You can see that now the data is different based on id passed at the top and this URL’s are user friendly and search engine friendly.

That is it. You can see it’s very easy to rewrite URL with ASP.NET MVC and in next chapter we will learn about test driven development with ASP.NET MVC.


Jalpesh
15 · 11% · 3478
8



Submit

1  Comments  

  • Hi Jalpesh,

    Good Job.

    Keep doing good work.

    • Dipak
    commented on Jun 19 2012 9:21AM
    Dipak
    2270 · 0% · 5

Your Comment


Sign Up or Login to post a comment.

"Getting Started with ASP.NET MVC - Part 9: URL Routing in ASP.NET MVC" rated 5 out of 5 by 8 readers
Getting Started with ASP.NET MVC - Part 9: URL Routing in ASP.NET MVC , 5.0 out of 5 based on 8 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]