Remove or hide .aspx from url in asp.net
In this article I will explain how to remove or hide .aspx from URL in asp.net. Remove or hide .HTML from url in asp.net OR Remove or hide .HTM from url in asp.net. if you want to remove or hide .aspx extension from url in asp.net then
you need to use URL routing concept in asp.net.
but if you don't want to use URL routing in your code then you can use following code.
The main purpose of this task is make URL as SEO friendly.
Remove html or htm from url using code.
Other Articles - Step by step solution
=> Add Global.asax file in your website or project
=> And add following code in your global.asax file
void Application_BeginRequest(object
sender, EventArgs e)
{
String WebsiteURL = Request.Url.ToString();
String[] SplitedURL = WebsiteURL.Split('/');
String[] Temp = SplitedURL[SplitedURL.Length -
1].Split('.');
// This is for aspx page
if (!WebsiteURL.Contains(".aspx")
&& Temp.Length == 1)
{
if (!string.IsNullOrEmpty(Temp[0].Trim()))
Context.RewritePath(Temp[0] + ".aspx");
}
/* // This is for HTML page
if (!WebsiteURL.Contains(".html") && Temp.Length == 1)
{
if (!string.IsNullOrEmpty(Temp[0].Trim()))
Context.RewritePath(Temp[0] +
".html");
}
// This is for HTM page
if (!WebsiteURL.Contains(".htm") && Temp.Length == 1)
{
if (!string.IsNullOrEmpty(Temp[0].Trim()))
Context.RewritePath(Temp[0] +
".htm");
}*/
}
=> Copy following div tag in your .aspx page , here you have to use page name only without .aspx extension
<div>
Remove
.aspx from asp.net website
<a href="Default">Click</a>
<%--Here write only page name
without extension --%>
=> You can also redirect page without extension
protected void Page_Load(object sender, EventArgs
e)
{
Response.Redirect("Default");
}