Url Rewriting and redirect to this url in browser

 Url Rewriting for hide extension and redirect to this url and execute it in browser

write down  this code in Global.asax file

 <%@ Application Language="C#" %>

<script RunAt="server">

void Application_BeginRequest(object sender, EventArgs e)
{
string curruntpath = Request.Path.ToLower();
curruntpath = curruntpath.Trim();
bool isredirected = curruntpath.EndsWith(".aspx");
bool isredirected2 = curruntpath.EndsWith(".html");
bool isredirected3 = curruntpath.EndsWith(".jpg");
bool isredirected4 = curruntpath.EndsWith(".jpeg");
bool isredirected5 = curruntpath.EndsWith(".gif");
bool isredirected6 = curruntpath.EndsWith(".png");
// string atr = curruntpath.Substring(curruntpath.IndexOf("/"));
if (!isredirected && !isredirected2 && !isredirected3 && !isredirected4 && !isredirected5 && !isredirected6)
{
HttpContext obj = HttpContext.Current;
string finalurl = curruntpath + ".aspx";
if (System.IO.File.Exists(Server.MapPath(finalurl)))
{
obj.RewritePath(finalurl);
}
else
{
obj.RewritePath("Error.aspx");
}
}
if (curruntpath.EndsWith(".aspx"))
{
HttpContext obj = HttpContext.Current;
obj.RewritePath("Tohideextentions.aspx");
}
}
void Application_Start(object sender, EventArgs e)
{
}
void Application_End(object sender, EventArgs e)
{
}
void Application_Error(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{
}
void Session_End(object sender, EventArgs e)
{
}
</script>


then execute your url then your extension hide and show your url like this

http://localhost:3231/UrlRewitingWithCode/Default2

and also execute your code in default.aspx page_load

 protected void Page_Load(object sender, EventArgs e)
{
Response.Write("The actual url is" + "<br/><br/>");
Response.Write(Request.Url.ToString());
Response.Write("<br/><br/>");
Response.Write("The Dummy url is" + "<br/><br/>");
Response.Write(Request.RawUrl.ToString());
Response.Write("<br/><br/>");
}


Ebook Download
View all
Learn
View all