Beware of Response.RedirectToRoute in MVC 3.0

ASP.Net MVC uses the new (to ASP.Net 3.5) Http*Base wrapper classes (HttpContextBase, HttpRequestBase, HttpResponseBase, etc) instead of the original Http* classes.  This allows you to create mock implementations that inherit the Http*Base classes without an actual HTTP request.  This is useful for unit testing, and for overriding standard behaviors (such as route checking).

In ordinary MVC code, the HttpContext, Request, and Response properties will return Http*Wrapper instances that directly wrap the original Http* classes (eg, HttpContextWrapper, which wraps HttpContext).  Most MVC developers use the HttpContext and related properties without being aware of any of this redirection.

Until you call Response.RedirectToRoute.  This method, which is new to .Net 4.0, redirects the browser to a URL for a route in the new ASP.Net routing engine.  Like other HttpResponse methods, HttpResponseBase has its own version of this method for derived classes to override.

However, in .Net 4.0, Microsoft forgot to override this method in the standard HttpResponseWrapper.  Therefore, if you call Response.RedirectToRoute in an MVC application (where Response is actually an HttpResponseWrapper), you’ll get a NotImplementedException.

You can see this oversight in the methods list for HttpResponseWrapper.  Every method except for RedirectToRoute and RedirectToRoutePermanent are list as (Overrides HttpResponseBase.MethodName().); these methods are listed as (Inherited from HttpResponseBase.MethodName().)

To work around this issue, you can either use the original HttpResponse by writing HttpContext.Current.Response.RedirectToRoute(…) or by calling Response.Redirect instead.

Note that most MVC applications should not call Response.Redirect or Response.RedirectToRoute at all; instead, they should return ActionResults by calling helper methods like return Redirect(…); or return RedirectToAction(…);

In the upcoming ASP.Net 4.5 release, these methods have been properly overridden.

15 comments:

The article you have shared here very good. This is really interesting information for me. Thanks for sharing!
192.168.1.1 login |gmail signin

As for me, this website is kinda comfortable for using. I don't understand your troubles cause I don't have the same. Probably, it's a matter of individual perception. Anyway, if you are the person who likes reading great articles
Play friv adventure games for free on internet

You wrote it well, I read it with pleasure.
Ariana

It's clear for me that this information will be useful after graduation. You can read more about college education here.

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

Quite an informative blog. Nowadays children spent so much time on their computer and laptop because now the Offline games famous and provide you free games and apps that can run without any internet needs. Try Offline games apk for more fun.

I am very happy that I can read these articles. Extra blog
Destinations for you

Now the information given in the article is already somewhat outdated. But a good education will always be relevant. Top tips on writing an essay from professionals.
CustomEssayMeister

Essays are common in middle school, high school and college, and you may even need to write essays in the business world (although they are usually called "reports" at that point). An essay is defined as "a short piece of writing that expresses information as well as the writer's opinion at https://essaysprofessors.com/ ."

This comment has been removed by the author.

Hello! Keep in mind that animal testing essay it is not a research paper, so your conclusion should not only restate the ideas discussed in the paper but also give the practical recommendations.

Such a piece of good information. I'm a student and of course, I want to have money and I decided to work as a freelance writer juvenile delinquency essay. Our essay writing service Essays-Writers.net has been rated the best in completing diverse writing tasks given to students at their educational institutions, such as colleges, high schools, and universities.

Post a Comment