33

Hi I am trying to redirect from a view to a different view but I get a red squigly in visual studio.How can I redirect from inside a view to another view.This is what I have tryed but it does not work:

@Response.Redirect("~/Account/LogIn?returnUrl=Products");

How can I redirect from my curent view to another view?

Nistor Alexandru
  • 5,309
  • 9
  • 46
  • 71

3 Answers3

43

It's because your statement does not produce output.

Besides all the warnings of Darin and lazy (they are right); the question still offerst something to learn.

If you want to execute methods that don't directly produce output, you do:

@{ Response.Redirect("~/Account/LogIn?returnUrl=Products");}

This is also true for rendering partials like:

@{ Html.RenderPartial("_MyPartial"); }
NKCSS
  • 2,716
  • 1
  • 22
  • 38
  • 2
    Thanks for answering. Sometimes there are legitimate reasons to redirect within a view, for example when there is no controller associated with the view. In my case, I have a mix of MVC pages as well as some legacy classic asp pages. Requests to default .asp documents eg domain.com/somefolder would be interpreted as MVC requests instead of .asp. After fiddling with default routes and ignoreRoute awhile with no results I concluded the easiest way was to create an MVC page at domain.com/somefolder and redirect to the asp page. – Louise Eggleton Jul 29 '14 at 20:43
  • Used in an emergency situation. No need to deploy again. – nrod Nov 25 '14 at 10:18
  • This came in handy for me in my Layout Page (that all my full Views share). If the user is not authenticated, then I Redirect to the Login page. This is not logic I want added to every Controller Action. I'm sure there's a more involved best-practice for this, but this is the only time I use a Redirect in a View/LayoutPage. – MikeTeeVee Dec 10 '18 at 12:29
19

That's not how ASP.NET MVC is supposed to be used. You do not redirect from views. You redirect from the corresponding controller action:

public ActionResult SomeAction()
{
    ...
    return RedirectToAction("SomeAction", "SomeController");
}

Now since I see that in your example you are attempting to redirect to the LogOn action, you don't really need to do this redirect manually, but simply decorate the controller action that requires authentication with the [Authorize] attribute:

[Authorize]
public ActionResult SomeProtectedAction()
{
    ...
}

Now when some anonymous user attempts to access this controller action, the Forms Authentication module will automatically intercept the request much before it hits the action and redirect the user to the LogOn action that you have specified in your web.config (loginUrl).

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • the way I have written my code I can not do that , if you take a look to this question I posted maybe you could give em an insight on how I can resolve my problem: http://stackoverflow.com/questions/14076921/duplicate-header-on-ajax-call – Nistor Alexandru Dec 29 '12 at 09:59
  • I've answered your other question as well. – Darin Dimitrov Dec 29 '12 at 10:01
  • Notice how I have decorated your controller action with the `[Authorize]` attribute and prevented the Forms Authentication module from redirecting to the LogOn page using the NuGet explained by Phil Haack (http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx). You could then very easily subscribe to the `.ajaxComplete()` global AJAX event handler for the document and intercept 401 status codes and simply redirect to the LogOn page on the client. – Darin Dimitrov Dec 29 '12 at 10:15
1

Purpose of view is displaying model. You should use controller to redirect request before creating model and passing it to view. Use Controller.RedirectToAction method for this.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459