As explained in my previous post, HttpResponse.Redirect performs the redirection by returning a 302 HTTP status code. A 302 response is generally indicative of “Moved Temporarily”. There is no default mechanism provided a return a different redirection code.
So how can we implement this. Short answer: using C# extension methods. Let’s look at some code below to understand this more clearly:
public static void Redirect(this HttpResponse response, int type, string url)
{
response.Clear();
switch (type)
{
case 301:
response.StatusCode = (int)HttpStatusCode.MovedPermanently;
response.StatusDescription = "Moved Permanently";
break;
case 302:
response.StatusCode = (int)HttpStatusCode.Found;
response.StatusDescription = "Found";
break;
case 303:
response.StatusCode = (int)HttpStatusCode.SeeOther;
response.StatusDescription = "See Other";
break;
case 304:
response.StatusCode = (int)HttpStatusCode.NotModified;
response.StatusDescription = "Not Modified";
break;
case 307:
response.StatusCode = (int)HttpStatusCode.TemporaryRedirect;
response.StatusDescription = "Temporary Redirect";
break;
default:
goto case 302;
}
response.RedirectLocation = url;
response.ContentType = "text/html";
response.Write("<html><head><title>Object Moved</title></head><body>");
response.Write("<h2>Object moved to <a href=\"" + HttpUtility.HtmlAttributeEncode(url) + "\">here</a>.</h2>");
response.Write("</body></html>");
response.End();
}
This code is elegant and simple (written by one very talented dev in my team). As you can see, we are building the response object in the function and are setting the correct status code and description for each specific case. We will discuss extension methods in detail in a later post.
Hello Nikhil,
ReplyDeleteThank you! Thank you! Thank you! Your blog was a total game changer!
When other tuning techniques fail to achieve the desired throughput or response time for the application, we need to consider analysing the code base and try optimizing it.
I'm learning to process sound in Java, and I thought I would start with the "javax.sound.sampled" package. I thought I would open a wav file and then play it through my computer's speaker.
I'm calling javax.sound.sampled.AudioSystem. getAudioInputStream(InputStream), and getting an exception, and now I'm feeling kind of lost. The exception is:
java.io.IOException: mark/reset not supported
I guess it means I need some kind of better AudioInputStream class (one which supports mark/reset), or maybe it doesn't like the wav file which I picked? (I just picked some random wav file out of a /Windows/Media directory, like "alarm01.wav".)
I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.
Obrigado,
Dan
Aivivu đại lý vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ
các chuyến bay từ mỹ về việt nam hiện này
Có chuyến bay từ Hàn Quốc về Việt Nam không
ve may bay tu canada ve viet nam
Your blog is extremely brilliant especially the quality content is really appreciable.
ReplyDeletebrand design consultancy