Sunday, February 16, 2014

URL routing in asp.net website

Introduction: In this article today I will explain how we can implement  URL routing in asp.net website similar as done in asp.net MVC application.

Description:
In the previous article I have explained Populate Dropdown List dynamically using Asp.net MVC RazorCode First migration in asp.net MVC 4, Populate Cascading Dropdown List in Asp.net MVC4 using Json and Jquery and What is Asp.net MVC? Its advantages and disadvantges.

URL routing provide the SEO friendly and clean url for application. To implement the clean url functionality in asp.net application follow the given steps:

Step 1:
Go to Tools>>Library Package Manager>>Package Manager Console and type the below given command:

Install-Package Microsoft.AspNet.FriendlyUrls

After its installation you get a successfully added message. Now you see in your application RouteConfig.cs and Site.Mobile.Master page has been added. See attached snapshot:
 
URL routing
(Click To Enlarge)
In VB:
You see the RouteConfig file in C#. To implement the routing in VB application you have to add RouteConfig.vb file instead of RouteConfig.cs and write the code:
Imports System.Web.Routing
Imports Microsoft.AspNet.FriendlyUrls

    Public Shared Sub RegisterRoutes(routes As RouteCollection)
        Dim settings = New FriendlyUrlSettings()
        settings.AutoRedirectMode = RedirectMode.Permanent
        routes.EnableFriendlyUrls(settings)
    End Sub

Note: You can delete/exclude the Site.Mobile.Master and ViewSwitcher.ascx files if you want.

Step 2:
Add a Global.asax file to application. Write the following code to file:
using System.Web.Routing;

protected void Application_Start(object sender, EventArgs e)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

In VB:
Imports System.Web.Routing

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        RouteConfig.RegisterRoutes(RouteTable.Routes)
End Sub


Build the application, run and check the result.

Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment