Haneef Puttur

ASP.net MVC Creating SEO friendly URL (Routing)

I want to pass 2 parameters to my controller and wants to display it int the razor view .

Task :  Change URL

From : http://localhost:50842/Contracts/Index?first=Line1&second=Line2

To       :  http://localhost:50842/Contracts/Index/Line1/Line2

My Controller:

My View :

My Classic method is : Pass the URL as query string :

http://localhost:50842/Contracts/Index?first=Line1&second=Line2

Here I am passing the parameters as above. and the controller will translate in the controller.

This request is handled by default route config file available at the visual studio mvc.

This is my current route file

 

If i run the project,  with above parameters my output will be as follows.

Now my task is i want to convert the URL into much user friendly as follows.

http://localhost:50842/Contracts/Index/Line1/Line2

This is simple and parameter names to be assigned by the route config.

To achieve this , i need to edit my route config fie as follows.

As you can see i have hard coded the parameter names in route file so that the moment i call the url , it will translate and assign as parameter names based on the positions.

If you run the project after making changes you will see the out put as follows.

 

By adding a simple route we can convert a non SEO URL into a SEO friendly simple URL.

 

 

 

 

 

 

 

 

 

 .

Exit mobile version