Just a simple redirect ASPX page

Published Aug 23 2019 08:45 AM 1,682 Views
Microsoft

You need to set a very simple redirect, from a specific location on your IIS web site or application, to a new URL? Then place this very simple default.aspx page at that location; in most of the cases, it works like a charm.

 

<%@ Page Language="C#"%>
<%
    Response.RedirectLocation = "http://new.location.net/optional/path";
    Response.StatusCode = 301;   //   301 = Moved Permanently   |   302 = Object moved   |   307 = Temporary redirect
%>

 

This is aimed at those of us too lazy to enable and use the HTTP Redirection feature of IIS. That would work with much better performance, and would cover some additional functionality, and doesn't need the .NET managed pipeline... But sometimes lazy is good :)

For those in need of complex redirects, the optional UrlRewrite module is THE solution, but you'll need to install and learn creating redirect rules.

Best of luck to all!

3 Comments
%3CLINGO-SUB%20id%3D%22lingo-sub-820277%22%20slang%3D%22en-US%22%3EJust%20a%20simple%20redirect%20ASPX%20page%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-820277%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20need%20to%20set%20a%20very%20simple%20redirect%2C%20from%20a%20specific%20location%20on%20your%20IIS%20web%20site%20or%20application%2C%20to%20a%20new%20URL%3F%20Then%20place%20this%20very%20simple%20%3CU%3E%3CSTRONG%3Edefault.aspx%3C%2FSTRONG%3E%3C%2FU%3Epage%20at%20that%20location%3B%20in%20most%20of%20the%20cases%2C%20it%20works%20like%20a%20charm.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CFONT%20color%3D%22%233366ff%22%3E%26lt%3B%25%40%20Page%20Language%3D%22C%23%22%25%26gt%3B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%233366ff%22%3E%26lt%3B%25%3C%2FFONT%3E%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Response.RedirectLocation%20%3D%20%3CFONT%20color%3D%22%23993300%22%3E%22http%3A%2F%2Fnew.location.net%2Foptional%2Fpath%22%3C%2FFONT%3E%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Response.StatusCode%20%3D%20301%3B%26nbsp%3B%26nbsp%3B%20%3CFONT%20color%3D%22%23008000%22%3E%2F%2F%26nbsp%3B%26nbsp%3B%20301%20%3D%20Moved%20Permanently%26nbsp%3B%26nbsp%3B%20%7C%26nbsp%3B%26nbsp%3B%20302%20%3D%20Object%20moved%26nbsp%3B%26nbsp%3B%20%7C%26nbsp%3B%26nbsp%3B%20307%20%3D%20Temporary%20redirect%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%233366ff%22%3E%25%26gt%3B%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20aimed%20at%20those%20of%20us%20too%20lazy%20to%20enable%20and%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fconfiguration%2Fsystem.webserver%2Fhttpredirect%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EHTTP%20Redirection%3C%2FA%3Efeature%20of%20IIS.%20That%20would%20work%20with%20much%20better%20performance%2C%20and%20would%20cover%20some%20additional%20functionality%2C%20and%20doesn't%20need%20the%20.NET%20managed%20pipeline...%20But%20sometimes%20lazy%20is%20good%20%3A)%3C%2Fimg%3E%3C%2FP%3E%0A%3CP%3EFor%20those%20in%20need%20of%20complex%20redirects%2C%20the%20optional%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fextensions%2Furl-rewrite-module%2Fusing-the-url-rewrite-module%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EUrlRewrite%3C%2FA%3Emodule%20is%20THE%20solution%2C%20but%20you'll%20need%20to%20install%20and%20learn%20creating%20redirect%20rules.%3C%2FP%3E%0A%3CP%3EBest%20of%20luck%20to%20all!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-820277%22%20slang%3D%22en-US%22%3E%3CP%3EA%20very%20simple%20ASPX%20page%20with%202%20lines%20of%20code%20may%20help%20you%20set%20a%20redirect%20from%20a%20specific%20location%3A%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200in%3B%20margin-left%3A%20.375in%3B%20font-family%3A%20Consolas%3B%20font-size%3A%2010.0pt%3B%20color%3A%20%2370ad47%3B%22%3EResponse.RedirectLocation%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fmhrinternational.sharepoint.com%2Fsecurityinformation%2F%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Ehttp%3A%2F%2Fnew.location.net%2Fpath%2F%3C%2FA%3E%22%3B%3CBR%20%2F%3EResponse.StatusCode%20%3D%20301%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%2F%2F%20Moved%20Permanently%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-SUB%20id%3D%22lingo-sub-821498%22%20slang%3D%22en-US%22%3ERe%3A%20Just%20a%20simple%20redirect%20ASPX%20page%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-821498%22%20slang%3D%22en-US%22%3E%3CP%3EI'd%20use%20a%20generic%20handler%20(ASHX)%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%26lt%3B%25%40%20WebHandler%20Language%3D%22C%23%22%20Class%3D%22Handler%22%20%26gt%3B%20%0A%20%20%0Ausing%20System%3B%20%20%0Ausing%20System.Web%3B%20%20%0A%20%20%0Apublic%20class%20Handler%20%3A%20IHttpHandler%20%20%0A%7B%20%20%0A%20%20%20%20public%20void%20ProcessRequest(HttpContext%20context)%20%20%0A%20%20%20%20%7B%20%20%0A%20%20%20%20%20%20%20%20context.Response.RedirectLocation%20%3D%20%22%3CA%20href%3D%22http%3A%2F%2Fnew.location.net%2Foptional%2Fpath%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fnew.location.net%2Foptional%2Fpath%3C%2FA%3E%22%3B%20%20%0A%20%20%20%20%20%20%20%20context.Response.StatusCode%20%3D%20301%3B%20%2F%2F%20301%20%3D%20Moved%20Permanently%20%7C%20302%20%3D%20Object%20moved%20%7C%20307%20%3D%20Temporary%20redirect%20%20%0A%20%20%20%20%7D%20%20%0A%20%20%0A%20%20%20%20public%20bool%20IsReusable%20%3D%26gt%3B%20false%3B%20%20%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENo%20need%20to%20load%20session%20state%20or%20go%20through%20the%20whole%20page%20life%20cycle.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20use%20Application_BeginRequest%20on%20global.asax%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eprotected%20void%20Application_BeginRequest(object%20sender%2C%20EventArgs%20e)%0A%7B%0A%20%20%20%20var%20context%3D%20((System.Web.HttpApplication)%20sender).Context%3B%0A%0A%20%20%20%20if%20(%2F*%20whatever%20condition%20*%2F)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20context.Response.RedirectLocation%20%3D%20%22%3CA%20href%3D%22http%3A%2F%2Fnew.location.net%2Foptional%2Fpath%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fnew.location.net%2Foptional%2Fpath%3C%2FA%3E%22%3B%20%20%0A%20%20%20%20%20%20%20%20context.Response.StatusCode%20%3D%20301%3B%20%2F%2F%20301%20%3D%20Moved%20Permanently%20%7C%20302%20%3D%20Object%20moved%20%7C%20307%20%3D%20Temporary%20redirect%0A%20%20%20%20%20%20%20%20context.Response.End()%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-822500%22%20slang%3D%22en-US%22%3ERe%3A%20Just%20a%20simple%20redirect%20ASPX%20page%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-822500%22%20slang%3D%22en-US%22%3E%3CP%3ETrue%2C%20%3CEM%3E%3CSTRONG%3EDefault.aspx%3C%2FSTRONG%3E%3C%2FEM%3Eis%20not%20the%20most%20performant%20solution.%20But%20it%20is%20very%20likely%20that%20a%20%22default.aspx%22%20entry%20exists%20in%20the%20Default%20Documents%20configuration.%20Your%20solution%20with%20Global.asax%20looks%20nice%3B%20I'm%20not%20sure%20if%20you%20can%20put%20a%20global.asax%20on%20any%20folder%20level%20in%20an%20app.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-822553%22%20slang%3D%22en-US%22%3ERe%3A%20Just%20a%20simple%20redirect%20ASPX%20page%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-822553%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20only%20have%20a%20global.asax%20at%20the%20root%20of%20the%20web%20application.%20That's%20what%20%2F*%20whatever%20condition%20*%2F%20is%20for.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20didn't%20mention%20the%20requirement%20for%20being%20on%20of%20the%20DefaultDocuments.%20But%20you%20can%20add%20Default%20Documents%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fconfiguration%2Fsystem.webserver%2Fdefaultdocument%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fconfiguration%2Fsystem.webserver%2Fdefaultdocument%2F%3C%2FA%3E)%20and%20you%20can%20also%20set%20a%20specific%20URL%20or%20extensions%20to%20a%20specific%20handler%20or%20handler%20factory%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fconfiguration%2Fsystem.webserver%2Fhandlers%2Fadd%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fiis%2Fconfiguration%2Fsystem.webserver%2Fhandlers%2Fadd%3C%2FA%3E).%3C%2FP%3E%3C%2FLINGO-BODY%3E
Version history
Last update:
‎Aug 23 2019 08:45 AM
Updated by: