In this Guest Blog, our ASP.net/IIS MVP, Brij Bhushan Mishra is elaborating FriendlyURLs which is one of the features of ASP.net.
Being an ASP.NET developer, you must have worked on ASP.NET Web Forms and ASP.NET MVC. One of the things in MVC, most of us like that is pretty and elegant URL. But this is not in the case of using ASP.NET web forms.
These URLs include crappy extensions like aspx. Many developers have written their custom URL writer or used some third party libraries to achieve it. And here we either end up paying some extra bucks for the third party library or writing custom URL writer which is itself painful task.
FriendlyURLs was introduced with ASP.NET 4.5 release 2 as a NUGET package and can be used with ASP.Net 4.0+.
Working with FriendlyURLs
To start working with the friendly URLs, we need to install it via NuGet in our project and require to add some code in various files and make the change. First let’s set up the environment.
Setting up FriendlyURLs in your application
To install using user interface, go to Tools -> Library Package Manager -> Manage NuGet Package for Solutions and search ‘FriendlyURLs’ and click on install as:
Once you have the NUGET package installed then we need to call a method RegisterRoutes from Application_Start method in Global.asax as
protected void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Now our application is set up with friendly URL when we’ll run our application, the URL will not contain file extension. Let’s run and check it.
The above URL does not contain the extension aspx.
Other useful Methods
To use these features, make sure you have added namespace Microsoft.AspNet.FriendlyUrls in your aspx.cs page.
URL segments here are the additional values in the URL that are not part of the path or the page. If I have an URL as http://localhost:53252/Personal/Home/Users/User/3; the path of the page is /Personal/Home.aspx. Rest are additional values that are passed with the URL and available as URL segments. Let’s see what does it return when we access it.
Here as we see that there are three values in URL segments. In the same way, we can pass multiple additional values through URL without using query string. And obviously, it makes URL pretty, cleaner and SEO friendly.
The above methods works only in case of the requested URL is of type of FriendlyUrl.
Leveraging some useful Classes
There are some classes that are very useful while working with this feature. Let’s us take FriendlyURL first.
Using FriendlyURL Class
This is a static class which is very useful in several scenarios such as:
So here we first pass the virtual path of the page and rest three parameters are passed as Url segments, we can pass as many as segments as we need.
Here we can see that the URL returned by the method is a Friendly URL.
Here we have just accessed this property and it returned the URL segments for that page. Even we did not require to pass any information about the page.
Using FriendlyUrlResolver
FriendlyUrlResolver is a class that provides another set of good features that can be used at various scenarios. To use it, first we need to create the instance of FriendlyUrlResolver. Here the constructor requires one parameter that is the extension of URL that need to take care while working on friendly URL: FriendlyUrlResolver friendlyUrl = new FriendlyUrlResolver(".aspx")
It provides many methods such as:
Here we have created the instance FriendlyUrlResolver with the parameter .aspx then we used ConvertToFriendlyUrl that returns the friendly URL. Here if we have provided some other extension while creating the instance then this method returns null so be careful while using it.
HttpContextWrapper wrapper = new HttpContextWrapper(HttpContext.Current);
HttpContextWrapper inherits from HTTPContextBase so we can get the extensions as
In this article, we discussed about FriendlyUrls and its various other components. It can be used with ASP.NET 4.0+ and easily configurable with the solutions. It provides a rich API to handle various scenarios. FriendlyUrlResolver class provides us capability to extend this feature based on our requirement.
About Guest Blogging
South Asia MVP Award Program introduces Guest Posts by the MVPs from the region. These posts would help readers to be in touch with the recent trends in technology and be up-to-date with knowledge on Microsoft products.
Author