I stumbled upon this while answering to this in stackoverflow. So thought of sharing how to do this ASP.NET webproject.
Expectation / End Result
- If you have Search.aspx file in project, it will automatically become SEO friendly, i.e., you can browse it like this “/Search” instead of “/Search.aspx”.
- If you need to pass parameters to that page, say product name, then you can do that using “/Search/Kindle” instead of “/Search.aspx?productname=Kindle”.
Steps to achieve this are listed below:
Step 1
Install “Microsoft.AspNet.FriendlyUrls
” from nuget package.
Open package manager console – Help. Then type the following:
Install-Package Microsoft.AspNet.FriendlyUrls
Step 2
Then it will automatically add the following in RouteConfig.cs.
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
Step 3
Add a webform with name say “Search.aspx”. And now if you browse http://www.example.com/Search, it will hit “Search.aspx”.
Now you are done with making SEO friendly URLS.
More Customization
Part – 1
If you want to make Search.aspx to be called as “Search-Product”, then you can do that using the following:
routes.MapPageRoute("", "Search-Product", "~/Search.aspx");
You need to add this to RouteConfig.cs, just after “routes.Enable
…”
Now, if you hit this URL – http://www.example.com/search-product, it will hit search.aspx.
Part -2
Now, you may need to pass parameters to Search.aspx. Yes you can do that, use the following line instead of the above.
routes.MapPageRoute("Find", "Search-product/{productname}", "~/Search.aspx");
To get value of productname
in Search.aspx, use the following “Page.RouteData.Values[“productname”]”
in page_load
or any other event in Search.aspx.
Example
I have created an example. Use the code suggested above. Hit the following URL:
Output in code is as follows:
Hope it works for all those following this blog.
CodeProject
Developing web based application since last 5 years, also interested in designing application with optimized used of available tech stacks, as well as apply those and real life experience to help out friends, colleagues,communities such as this or other forums.
MCSD (2013) certified developer.
Reach me - http://about.me/arindamnayak