URL rewrining using web.config and URL rewriting using global.asax
URL rewrining using web.config:
URL rewriting in web.config is only use for small number of pages. Here you have to rewrite every page manually in web.config. I am writing sample code for 4 urls.
xml version="1.0"?>
<configuration>
<urlMappings enabled="true">
<add url="~/About-company.aspx"
mappedUrl="~/index.aspx?id=1" />
<add url="~/About-products.aspx"
mappedUrl="~/index.aspx?id=2" />
<add url="~/Contact-us.aspx"
mappedUrl="~/index.aspx?id=3" />
<add url="~/our-team.aspx"
mappedUrl="~/index.aspx?id=4" />
urlMappings>
. . .
configuration>
URL rewriting using global.aspx:
This very simple way and very use full. In this way we can rewrite N number of pages and there is no need extra server configuration. Only you have to use Application_BeginRequest event. Inside this event use Context.RewritePath method to set which URL will execute internally in code behind. Here is the code:
string NewsID = CurrentPath.Substring(CurrentPath.IndexOf("/"));
HttpContext MyContext = HttpContext.Current;
MyContext.RewritePath("/news-show.aspx?News=" + NewsID);
}
}
URL rewrining using web.config:
URL rewriting in web.config is only use for small number of pages. Here you have to rewrite every page manually in web.config. I am writing sample code for 4 urls.
xml version="1.0"?>
<configuration>
<urlMappings enabled="true">
<add url="~/About-company.aspx"
mappedUrl="~/index.aspx?id=1" />
<add url="~/About-products.aspx"
mappedUrl="~/index.aspx?id=2" />
<add url="~/Contact-us.aspx"
mappedUrl="~/index.aspx?id=3" />
<add url="~/our-team.aspx"
mappedUrl="~/index.aspx?id=4" />
urlMappings>
. . .
configuration>
URL rewriting using global.aspx:
This very simple way and very use full. In this way we can rewrite N number of pages and there is no need extra server configuration. Only you have to use Application_BeginRequest event. Inside this event use Context.RewritePath method to set which URL will execute internally in code behind. Here is the code:
void Application_BeginRequest(object sender, EventArgs e)
{
// Get the current path
string CurrentURL_Path = Request.Path.ToLower();
if (CurrentURL_Path.StartsWith("/news/"))
{
CurrentURL_Path = CurrentURL_Path.Trim("/");{
// Get the current path
string CurrentURL_Path = Request.Path.ToLower();
if (CurrentURL_Path.StartsWith("/news/"))
{
string NewsID = CurrentPath.Substring(CurrentPath.IndexOf("/"));
HttpContext MyContext = HttpContext.Current;
MyContext.RewritePath("/news-show.aspx?News=" + NewsID);
}
}
0 comments:
Post a Comment