Home > Enterprise >  How to get routeUrl of Custom route value in Razor Pages
How to get routeUrl of Custom route value in Razor Pages

Time:10-10

This question will be two questions

I have changed the values of my routes using the @page directive via Microsoft's documentation. As an example I have changed one of my routes with the filename Landing.cshtml to @page "/". Now I am trying to get the value of the new custom route that I have defined as I would like to send the user back to that page pending direction from my signup logic like so:

returnUrl = Request.Form["returnUrl"];

Then

return LocalRedirect(returnUrl);

I am passing in the returnUrl from a from that is posting to this PageModel via the below input

<input type="hidden" id="returnUrl" name="returnUrl" value='@((string) ViewContext.RouteData.Values["Page"])' />

Yet the value always is the defaul /Landing and not the custom value I defined on the page of /.
Question 1: How do you go about sending in the custom page url without hard coding it into the form value itself
Question 2: What is the difference between ViewContext.RouteData.Values and RouteData.Values, they both have the same keys and values.

CodePudding user response:

Question1

You can get the Name of the Route like this:

// it only returns the name, so you have to add the "/"
var routeName = "/"   ViewContext.ActionDescriptor.AttributeRouteInfo.Template;

// @page "/" --> ""
// @page "/foobar" --> "foobar"

Question 2

I am not sure but i think there is no difference. It is the same type of object. ViewContext.RouteData are the Values for the Request and RouteData alone is mentioned as Values for the Action according to Intellisense.

  • Related