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.

CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • 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. https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/ . 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? – ADyson Dec 14 '17 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](https://stackoverflow.com/questions/46096068/asp-net-core-2-0-creating-urlhelper-without-request) – SpruceMoose Dec 14 '17 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. – CBreeze Dec 14 '17 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 – CBreeze Dec 14 '17 at 14:09
  • MVC or Web API project? – ADyson Dec 14 '17 at 14:11
  • @ADyson Sorry, MVC. – CBreeze Dec 14 '17 at 14:14
  • @CBreeze: What kicks the process of generating the URL off, is it a web request? If yes, look at this post: [Injection of IUrlHelper in ASP.NET Core](https://stackoverflow.com/questions/37322076/injection-of-iurlhelper-in-asp-net-core-1-0-rc2) – SpruceMoose Dec 14 '17 at 14:50

1 Answers1

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 });