1

I want to create a Url that I can email that goes to an action within a controller, passing to the action an Id and a Token. This is my code so far:

var action = new UrlActionContext
{
    Action   = "Verify",
    Controller = "Auth",
    Values = id, token
};
var url = UrlHelperExtensions.Action(action);

UrlHelperExtensions.Action is expecting an IUrlHelper however, and I have not been able to get this to work using that interface. Can someone please explain how I am able to form a Url that goes to this action?

I can only seem to find solutions to Asp.Net MVC projects, whereas I am using Asp.Net Core 2.0.

7
  • 1
    .NET Core is not analogous to .NET MVC. .NET Core is a new version of the .NET, alongside the older .NET Framework, and Xamarin. The .NET Standard library is starting to provide a common API across all of them. blogs.msdn.microsoft.com/dotnet/2016/09/26/… . You can create an MVC app based on the .NET Core framework, or an API, or most other kinds of program. So it's relevant to ask what kind of app you are trying to create a URL for? Commented Dec 14, 2017 at 13:11
  • What type of application is generating this URL and in what context? If you are creating this url in a background process (without access to a request) then you may struggle to create a IUrlHelper cleanly - see ASP.Net Core 2.0: Creating UrlHelper without request Commented Dec 14, 2017 at 13:40
  • @ADyson This is an Asp.Net Core 2.0 application. When the user clicks register I want to generate a Url with their Id and Token, pointing to an action on a Controller, all within the webapp. Commented Dec 14, 2017 at 14:02
  • @CalC I'm not sure if it's the vagueness of my question but I thought there would be a relatively simple solution. I just want to generate a url something like `localhost:12345/Auth/VerifyEmail=UserId,Token Commented Dec 14, 2017 at 14:09
  • MVC or Web API project? Commented Dec 14, 2017 at 14:11

1 Answer 1

2

For ASP.Net Core 2.0 IUrlHelper is available as a property of the controller. ControllerBase.Url is an IUrlHelper instance. You should be using it like this:

var url = Url.Action(nameof(DoSomething), new { id = 10 });

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.