Simple 301 Redirects ASP.NET 4.0 in Code
Sometimes it is just easier to code in 301 redirects into an application that you are building. Specially if it is replacing a system on the same domain.
I had this recently with https://carquote.quoteline.ie.
It is very easy to use your Global.asax file to program in 301 redirects without having to use IIS when using ASP.NET 4.0.
void Application_BeginRequest(object sender, EventArgs e)
{
// old quoteline system 301 redirects
string localUrl = Request.Url.LocalPath;
ArrayList redirectUrls = new ArrayList();
redirectUrls.Add(“motor1.aspx”);
redirectUrls.Add(“motor2.aspx”);
redirectUrls.Add(“finalQuoteAll.aspx”);
redirectUrls.Add(“QuoteAssumptions.aspx”);
foreach (string url in redirectUrls)
{
if (localUrl.IndexOf(url, StringComparison.OrdinalIgnoreCase) >= 0)
{
HttpContext.Current.Response.RedirectPermanent(“https://carquote.quoteline.ie/”);
}
}
}
If you have more complex URLs that contain query string data that must be maintained @improvedk has a fairly good article about that… review the resource link.
Resource:
