0

If I have a "page name" or "route name", and also route values, then I could create a URL in various ways:

return RedirectToPage
return RedirectToRoute
Url.Action
Url.ActionLink
Url.Link
Url.PageLink
Url.RouteUrl
LinkGenerator.GetPathByAction
LinkGenerator.GetPathByName
LinkGenerator.GetPathByPage
LinkGenerator.GetPathByRouteValues
LinkGenerator.GetUriByAction
LinkGenerator.GetUriByName
LinkGenerator.GetUriByPage
LinkGenerator.GetUriByRouteValues

But suppose I already have a URL path (e.g. /customer/edit) and route values (an anonymous object or a dictionary, e.g. id which equals 1) then how do I create a URL? I can't use return Redirect(path) as it doesn't support route values.

I'm looking for a RedirectXXX("/customer/edit", new { id = 1, name = "foo" }) method, if one exists. I'm surprised I could find so many overrides for so many use cases, but not one for the simplest use case.

(Yes, I can write my own, but I'm assuming something like this already exists; does it?)

1 Answer 1

0

A workaround, which could be put into an abstract base class like PageModelBase:

public RedirectResult Redirect(string urlPath, IDictionary<string, string?> routeValues)
{
  // ...input validation elided
  var urlFull = QueryHelpers.AddQueryString(urlPath, routeValues);
  return Redirect(urlFull);
}

However, I prefer a built-in solution, which also caters for an anonymous object (instead of a dictionary) without JSON (de)serialisation hacks.

If there is such a thing, please post it and I'll accept your answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.