locked
Redirecting in razor pages RRS feed

  • Question

  • User-1282397908 posted

    In razor there are different redirect methods (all with various overloads):

    • Redirect
    • RedirectToPage
    • RedirectToAction
    • RedirectToRoute

    I want to redirect a user from the /Index pagemodel to the 'Clients/Index' page, the following redirects work:

    • Redirect("/Clients") or "./Clients" or "Clients" or ./Clients/Index or ....
    • RedirectToPage("Clients/Index"), other variants starting with '/' or './' also work as long as "/Index" is added to the page name

    Do not work:

    • RedirectToAction("/Clients"): This returns me to the same page I'm on (/Index) resulting in a never ending loop (the same happens for "./Clients", "Clients", "/Clients/Index",....
    • RedirectToPage("/Clients"): RedirectToPage only workis if /index is explicitly added, otherwhise error: InvalidOperationException: No page named '/Clients' matches the supplied values.
    • RedirectToRoute("..."): no combination works

    So the question now are:

    • Why to RedirectToAction and ToRoute exist in razor pages, to me they seem like MVC style methods
    • Why does Redirect accepts a url without Index and RedirectToPage doesn't?
    • Which redirect should be used? (is there any recommendation)?
    Friday, July 26, 2019 12:08 PM

Answers

  • User475983607 posted

    I can make some assumptions on what a page name and an action are but writing code on assumptions.... If you insist that it is documented than the question becomes what are a pagename and an actionname in the context of razorpages.

    An action name is the name of the Controller Action.   The page name is the name of the Razor page.  Actions and Razor pages use different routing schemes so it is important make sure you use the redirect method that goes with the framework. 

    For example the following is copied the documentation.

    Parameters

    actionName
    String

    The name of the action.

    And the other question still stands are there any benefits / downsides to using either of these methods (performance, security, error handling,.....)

    A redirect is an HTTP 302 response sent to the browser.  The HTTP response has a location header that tells the browser what URL to GET next. 

    https://en.wikipedia.org/wiki/HTTP_302

    The redirect methods all resolve to a 302 HTTP response but are slightly different to support the each framework.   The Redirect has been around a long time.  It takes a path and generates a 302.  The RedriectToAction is used in MVC and redirects to an Action using the route logic for MVC.  The RedirectToPage is for use in Razor Pages and understands Razor Page routing.  Finally, the RedirectToRoute redirects to a named route where the named route contains all the information to generate a specific URL.

    The ASP.NET Core fundamental docs cover routing which will help with your understanding of the redirect.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2

    The second part of your question is loaded with assumptions.   Basically, use the right redirect for the task at hand.  Security, performance, and error handing are not relevant.  All the redirect methods return a 302 so from a performance perspective they are equal.  A stated above, a redirect causes the browser to do an HTTP GE.  Security is driven by your security requirements.  It is up to you to secure the GET Action or Razor Page according your security requirements.  Exception handling is driven by your exception handling requirements.  It's up to you to add try...catch block to the Actions or Razor Pages that handle  HTTP GET.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 26, 2019 2:45 PM

All replies

  • User475983607 posted

    RedirectToAction("/Clients"): This returns me to the same page I'm on (/Index) resulting in a never ending loop (the same happens for "./Clients", "Clients", "/Clients/Index",....

    You misunderstand how to use RedirectToAction().  You're entering a path when the method input expects an action name.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2

    RedirectToPage("/Clients"): RedirectToPage only workis if /index is explicitly added, otherwhise error: InvalidOperationException: No page named '/Clients' matches the supplied values.

    Same as above, you're not using the method as indented.

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.razorpages.pagebase.redirecttopage?view=aspnetcore-2.2

    It helps to read the reference documentation.

    Friday, July 26, 2019 12:55 PM
  • User-1282397908 posted

    mgebhard, thanks for your reply. I do agree with you that reading the documentation is a good idea and as such I have read it (those exact pages).

    But lets be honest, nobody needs documentation to know that a method 'RedirectToPage(string pagename)' will redirect me to a page with that name. Such documentation is a waste of paper/disk space and time (both from the reader and the writer).

    I can make some assumptions on what a page name and an action are but writing code on assumptions.... If you insist that it is documented than the question becomes what are a pagename and an actionname in the context of razorpages.

    And the other question still stands are there any benefits / downsides to using either of these methods (performance, security, error handling,.....)

    Friday, July 26, 2019 1:38 PM
  • User475983607 posted

    I can make some assumptions on what a page name and an action are but writing code on assumptions.... If you insist that it is documented than the question becomes what are a pagename and an actionname in the context of razorpages.

    An action name is the name of the Controller Action.   The page name is the name of the Razor page.  Actions and Razor pages use different routing schemes so it is important make sure you use the redirect method that goes with the framework. 

    For example the following is copied the documentation.

    Parameters

    actionName
    String

    The name of the action.

    And the other question still stands are there any benefits / downsides to using either of these methods (performance, security, error handling,.....)

    A redirect is an HTTP 302 response sent to the browser.  The HTTP response has a location header that tells the browser what URL to GET next. 

    https://en.wikipedia.org/wiki/HTTP_302

    The redirect methods all resolve to a 302 HTTP response but are slightly different to support the each framework.   The Redirect has been around a long time.  It takes a path and generates a 302.  The RedriectToAction is used in MVC and redirects to an Action using the route logic for MVC.  The RedirectToPage is for use in Razor Pages and understands Razor Page routing.  Finally, the RedirectToRoute redirects to a named route where the named route contains all the information to generate a specific URL.

    The ASP.NET Core fundamental docs cover routing which will help with your understanding of the redirect.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2

    The second part of your question is loaded with assumptions.   Basically, use the right redirect for the task at hand.  Security, performance, and error handing are not relevant.  All the redirect methods return a 302 so from a performance perspective they are equal.  A stated above, a redirect causes the browser to do an HTTP GE.  Security is driven by your security requirements.  It is up to you to secure the GET Action or Razor Page according your security requirements.  Exception handling is driven by your exception handling requirements.  It's up to you to add try...catch block to the Actions or Razor Pages that handle  HTTP GET.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 26, 2019 2:45 PM
  • User-821857111 posted

    Why to RedirectToAction and ToRoute exist in razor pages, to me they seem like MVC style methods
    Razor Pages is built on top of MVC. A number of MVC-specific methods poke through, but are of no use/rarely useful in Razor Pages.

    Why does Redirect accepts a url without Index and RedirectToPage doesn't?
    Covered in previous answers

    Which redirect should be used? (is there any recommendation)?
    In a Razor Pages app, use RedirectToPage for a 302 (temporary/PRG) redirect, and RedirectToPagePermanent for a 301 redirect.

    Friday, July 26, 2019 6:40 PM