Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.NET MVC, how does response.redirect work?

I have used response.redirect in classic ASP and ASP.NET webforms. However, with MVC 2.0, I am running into something peculiar.

I have a private method in a controller class that is used by multiple controller methods to help load and validate some information. This private method is setup to redirect if a problem is discovered to a generic error message page.

The big problem I am noticing is that the calling controller class and page view attempt to complete rendering and loading before the redirect actually takes place. This is annoying in development because the View throws exceptions that I need to ignore before my generic error page finally loads.

As mentioned above, I am used to the older model of response.redirect which prevented subsequent code on a page from being executed as the new page would then load.

Any help or advice on redirects in MVC would be greatly appreciated.

like image 408
Swoop Avatar asked May 12 '10 20:05

Swoop


People also ask

What is response redirect in MVC?

Response.Redirect. Transfers the page control to the other page, in other words it sends the request to the other page. Causes the client to navigate to the page you are redirecting to. In http terms it sends a 302 response to the client, and the client goes where it's told.

How do I use response redirect?

Response. Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister. aspx" page and it has a button that redirects you to the "UserDetail.

Does response redirect stop execution?

When you use Response. Redirect("Default. aspx",true ) which is by default true then the execution of current page is terminated and code written after the Response.

What is response redirect and server transfer in asp net?

So, in brief: Response. Redirect simply tells the browser to visit another page. Server. Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.


1 Answers

The conventional mechanism to redirect in ASP.Net MVC is to return an object of type RedirectResult to the client. If this is done before your View method is called, your view methods will never be called.

If you call Response.Redirect yourself, instead of letting Asp.Net MVC's front controller do that for you, your controller method will continue on until it finishes or throws an exception.

The idiomatic solution for your problem is to have your private method return a result that your controller can use.

for example:

public ActionResult Edit(MyEntity entity) {   if (!IsValid()) return Redirect("/oops/");   ...   return View();  }  private bool IsValid() {   if (nozzle==NozzleState.Closed) return false;   if (!hoseAttached) return false;   return (userRole==Role.Gardener); } 
like image 66
JasonTrue Avatar answered Sep 28 '22 05:09

JasonTrue