In case, your clients asks you to make their website URL , SEO friendly or sometime they give you specific choice. Example- They might as you to make their website URL as *.html *.(anything). So, dealing with such scenario is little complicated. You have more options to do , like using any third party dlls. But, Lets do it using ASP.Net 4.0.
Lets see how can we make URL as *.html using ASP.NET 4.0
1. In Global.asax write
void Application_Start(object sender, EventArgs e)
{
// log4net.Config.DOMConfigurator.Configure();
// Code that runs on application startup
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
2. Here is the body of RegisterRotues
public static void RegisterRoutes(System.Web.Routing.RouteCollection routeCollection)
{
routeCollection.Add("MyRoute", new System.Web.Routing.Route("{id}.html", new MyProjectRouting()));
}
Third parameter is important, this is a class i.e MyProjectRouting which is being implemented by IRouteHandler
3. MyProjectRouting class is defined below
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string path = string.Empty;
try
{
Boolean isvalidProdId = false;
string pId = requestContext.RouteData.Values["id"] as string;
HttpContext.Current.Items["id"] = pId;
// you will have your id here now you can give your actual URL based on the id , where you want your page to redirect . put your actual path . for instance - path="~/Pages/Default.aspx"
}
catch (Exception ex)
{
log.Debug("Error in Routing" + ex.ToString());
}
return BuildManager.CreateInstanceFromVirtualPath(path, typeof(Page)) as Page;
}
Note: * can be anything , can be your id
Now, we will see how we are going to navigate from one page to another. Assuming , there is hyperlink and you want to redirect to Second.aspx from First.aspx. What would be NavigateUrl in Hyperlink. - Here it is
On First.aspx Write
<a class="category-products1-link" href="test.html">Test me </a>
How can we access this id in Second.aspx
- On Page_Load
string id= Convert.ToString(Page.RouteData.Values["id"]);
This is how you can make your Url as *.html
Note: Please feel free to add more if you want to
Now, we will see how we are going to navigate from one page to another. Assuming , there is hyperlink and you want to redirect to Second.aspx from First.aspx. What would be NavigateUrl in Hyperlink. - Here it is
On First.aspx Write
<a class="category-products1-link" href="test.html">Test me </a>
How can we access this id in Second.aspx
- On Page_Load
string id= Convert.ToString(Page.RouteData.Values["id"]);
This is how you can make your Url as *.html
Note: Please feel free to add more if you want to