-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it easy to configure a host/scheme for absolute URLs with LinkGenerator #14192
Comments
I agree. This should be less cumbersome. I don't care if it's IUrlHelper or IUrlGenerator, as long as it is fully setup by the DI system as part of ASP.NET. |
This is not true. LinkGenerator is specifically designed to work without an HTTP context. See: https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.Core/src/Routing/ControllerLinkGeneratorExtensions.cs#L84 and similar methods. |
As far as I can work out this is not true for absolute URL's containing the scheme, host and port. Using this code: this.linkGenerator.GetPathByRouteValues("GetPage", new { First = 1, Last = 10 }); Outputs:
|
@RehanSaeed - can you provide some more information? I'm not sure how I'm supposed to draw a conclusion from that. |
I want to generate absolute URL's with the full scheme, host and optionally a port i.e. Today, if you want to do this, you have to have the That's all well and good in a controller where you have easy access to the Both of these solutions are noisy and require extra work, just to get an absolute URL. Ideally, I'd want a method that does all of that for me and allows me to generate URL's without all the extra noise and ceremony. Does that clear things up? |
I agree with RehanSaeed. I had the need to generate absolute urls in a module that sends out emails to clients (which is outside the web project). I only want the IUrlHelper or LinkGenerator as dependency and don't want to do all the IHttpContextAccessor setup as described above. |
This is something we can work with. Do you actually care about the current request? Or do you just want it to be easier to generate absolute URLs? I can imagine a feature where you can centrally configure a default scheme and host that will be used in URLs - or it could fallback to the listening address if nothing is configured and there is only one listening address. There reason I'm making this distinction is that passing in the "current request" does more than just set up scheme/host/pathbase - it also includes ambient route values. Trying to make a service that will magically use ambient values from the current request is a recipe for failure because it's going to do different things depending on which route reached the current thing. |
I don't care about the current request at all, I'm just forced to care to generate a URL based on the current requests scheme, host and URL.
I could get behind such a feature. The fallback sounds like the correct default with the option to override it. |
That sounds good to me as well! |
Great, I'm sticking this in the 5.0 milestone so it will get looked at. |
We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
I have a requirement to use the LinkGenerator to generate links outside of a Web Project, similar to the requirement mentioned by @FilipVanBouwel earlier in this issue. My requirement is to use the LinkGenerator in a worker process that runs in its own process, with no web host configured. The only way I have been able to achieve this so far is as follows. Are there any issues with using this method? Is there a simpler approach that I am overlooking?
|
Thanks for contacting us. We're moving this issue to the |
Is your feature request related to a problem? Please describe.
LinkGenerator
LinkGenerator
requires theHttpContext
to generate a full absolute URL which also contains the scheme, hostname and optional port. Outside of the controller, this means I have to also inject theIHttpContextAccessor
into my constructor and pass it in like so:This requires a lot of ceremony and additional noise.
IUrlHelper
IUrlHelper
does not require theHttpContext
if you add this code to theStartup
. We use theIActionContextAccessor
andIUrlHelperFactory
to registerIUrlHelper
into the IoC container.Then if you use the following extension method to create your URL's:
Here is the usage:
I think you'll agree this is much nicer.
Describe the solution you'd like
I would like a way to generate URL's without having to inject the
IHttpContextAccessor
andLinkGenerator
just to generate a single URL. Or a way to useIUrlHelper
without all the setup code to get it working nicely.The text was updated successfully, but these errors were encountered: