Replies: 8 comments
-
Yes, I'm aware of this. I believe I tried to use it at one point but can't remember why I stopped. I'll check and come back to this issue. |
Beta Was this translation helpful? Give feedback.
-
As far as I can work out, to generate a URL using this.linkGenerator.GetUriByRouteValues(
CarsControllerRoute.GetCarPage,
new PageOptions() { First = pageOptions.First, Last = pageOptions.Last, After = endCursor },
Uri.UriSchemeHttps,
new HostString("todo-get-host-from-httpcontextaccessor")); The alternative with this.urlHelper.AbsoluteRouteUrl(
CarsControllerRoute.GetCarPage,
new PageOptions() { First = pageOptions.First, Last = pageOptions.Last, After = endCursor }); I'd really like to make the switch to simplify the template. If there is something I'm missing and there is a way to use the current URL's schema and host with |
Beta Was this translation helpful? Give feedback.
-
Feel free to reopen if you find anything to improve the situation. |
Beta Was this translation helpful? Give feedback.
-
@RehanSaeed this.linkGenerator.GetUriByRouteValues(
this.httpContextAccessor.HttpContext,
CarsControllerRoute.GetCarPage,
new PageOptions() { First = pageOptions.First, Last = pageOptions.Last, After = endCursor }); Since we did the IOC register for HttpContextAccessor anyway, injecting it and use itexplicitly like this is clearer. However, if you want to hide the httpcontext for the sake of brevity, you could always have something to inject HttpContext similarly to what your |
Beta Was this translation helpful? Give feedback.
-
I missed that overload. I agree that it's a bit better than the one I posted. It's a shame that you have to pass in the With What are your thoughts, do you still think we should use it? |
Beta Was this translation helpful? Give feedback.
-
Sorry that for a late reply. In my opinion, most of the case when the user code wants to generate the link, it needs to have access to HttpContext too. The only scenario that hiding HttpContext from user code may come handy is when you want to implemented some helper extension method like you've done with That's all for my feedback as a user. I'll leave the final decision for you. |
Beta Was this translation helpful? Give feedback.
-
Honestly, you have compelling arguments and I'm in two minds about it. There is no perfect solution unfortunately. So, I've raised this issue, lets see what the ASP.NET Core team say: |
Beta Was this translation helpful? Give feedback.
-
After receiving a very positive response in that issue, I've switched the project to use |
Beta Was this translation helpful? Give feedback.
-
Which project template is the issue about?
ApiTemplate
...
Which version of the project template is the issue about?
2.2.0
...
Which version of Visual Studio or the dotnet SDK were you using?
2.2.401
...
In Asp.net Core 2.2, a singleton service called LinkGenerator is introduced. Currently, it behaves similar to IUrlHelper. Instead of manually register the
IUrlHelper
inStartup.cs
like thisthe template should use LinkGenerator instead.
References:
Beta Was this translation helpful? Give feedback.
All reactions