This is going to be my last post of 2012. I am very happy that I am writing about a very exciting feature that comes with ASP.NET.
I am really excited to learn and write about this. I really hated the ugly URLs and the extensions of the page that were always visible when one opens the application until and unless you write your own custom URL processor. With ASP.NET 4.0, Routing was introduced, which provided the capability to ASP.NET to have easier, manageable and SEO friendly urls, I wrote a post on it. You can click below to have a look at it.
But that is a little tidy to write. And it looks like writing extra code that does not produces any business functionality.
FriendlyURL
as the name suggests gives a clean and SEO friendly URL. Currently, it is available via NuGet and in pre release version.
So for this post, I have used my Visual Studio 2012 IDE. I created a new project as File-> New -> Website -> ASP.NET Empty Website.
Once the project gets created, I require to install the NuGet for my application. So go to Tools-> Library Package Manager-> Package Manager Console.
It opens a console, then type Install-Package Microsoft.AspNet.FriendlyUrls -Pre
and press enter as:
As you can see, I typed the command and it can be seen in the first red encircled area and press enter. It’ll install the NuGet and you can see the last red encircled area which shows FirendlyUrl
is successfully added.
This can be used with two ASP.NET versions: ASP.NET 4.5 and ASP.NET 4.0.
Now you are ready to work with friendly URLs.
If you have read the Readme.text, it gives a clear idea how to start with. But I’ll take you through the steps. So let’s start.
Step 1
Add and create a Class name, preferably RouteConfig.cs (You can have any name. I used MyRouteConfig.cs) and make it static
. Also add the namespace in the class Microsoft.AspNet.FriendlyUrls
.
Step 2
Create a static
function in the class preferably named as RegisterRoutes
with a parameter of type RouteCollection
and add a line routes.EnableFriendlyUrls()
in the method as:
public static class MyRouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.EnableFriendlyUrls();
}
}
Step 3
Add Global.asax file and add the following line in Application_Start
as:
void Application_Start(object sender, EventArgs e)
{
MyRouteConfig.RegisterRoutes(RouteTable.Routes)
}
Now you can start the development of your application. I have created two pages, First.aspx and Second.aspx in my solution.
That’s it. Now your code is ready to run.
If you see the circled area in the above picture, it does not contain page extension (aspx). So now the url looks pretty. As an end user, I don’t care about the page extension and even sometimes, it just irritates me.
Now if you want to redirect to another page, you can redirect by typing URL without page extension as:
Response.Redirect("~\\Second");
There is one caveat here. You cannot have a folder and Page with the same name at the same level. If you have the name of a folder and the name of aspx page at the same level, you would not be able to access the page without extension. So please keep it in mind.
Now you must have a question in Querystring
. What will happen with Querystring
?
You don’t need to worry at all. QueryString
s will work as earlier. Let’s have a look to the URL http://localhost:57353/First?Id=1001, then you can read the QueryString
as below similar to the earlier:
string Id = Request.QueryString["Id"] as string;
Next point, if I have a URL say http://localhost:57353/First/Products/NewProduct and there is no page as NewProduct
. What will happen. It’ll open First.aspx.
But, how does it work?
It starts from last segment (NewProduct
) and will check if there is any NewProduct.aspx. If not, then it moves to the next segment and continues till it finds the exact aspx page.
Let’s explore more. Go to the server side of the page and include the namespace Microsoft.AspNet.FriendlyUrls
. You’ll get a bunch of methods that will be very useful at certain times.
Request.GetFriendlyUrlSegments()
– It returns a collections of string
which represents the URL segments. So if I have a URL http://localhost:57353/First/Products/NewProduct and the page exists First.aspx, then it returns.
So you can see above, there are two segments and one can iterate through it. These url segments can be used to pass the values to a page via URL instead of QueryString
. It also looks pretty.
Request.GetFriendlyUrlFileVirtualPath()
– It returns the File virtual path. For the URL, discussed in the above example it will show as:
You can see it shows the Virtual path of the file.
Request.GetFriendlyUrlFileExtension()
-It returns the extension of the file. For the above url, it will be shown as:
As you can see, the extension of the page. As now with friendly url, the page extension is not shown. You can get the extension by the above method.
There are other important methods/features available that can be used to generate the URL, etc. I’ll discuss some of those here.
An available class FriendlyURL
provides few static
functions/properties that are very useful. Let’s see.
Hope you must have liked this new coming feature of ASP.NET. This will let you get rid of aspx extension in the URL. Frankly speaking, during the earlier days of my career, I used to see the extension to check if the website is developed in ASP.NET.
I find this feature as one of the most of the exciting features of ASP.NET. Hope all of you enjoy it and also like the post.
Enjoy and love ASP.NET. And a very Happy New Year to all.
Cheers!