You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following on from the useful discussion in PR #949 I have implemented a custom ILinkBuilder that uses the MVC routing data to help generate links rather than just string building from static config.
Use cases where this is required:
Your app uses a global path base such as in startup.Configure app.UsePathBase(new PathString("/api"))
If the path base is a fixed string then json api users must also configure the JsonApiOptions.Namespace option to prefix all links, but with the route aware link builder, this option is no longer required and all links will include path base by default
Your app is using some custom routing convention, such as the multi-tenant setup described in task: make [Attr] and RequestDeserializer.SetAttributes open to extension #949 like GET /api/{tenantId}/books. The current LinkBuilder cannot see/understand the path before the resourceName, so all links start /books rather than the complete link. I have also used other weird routing setups that mess up the links such as /{controller}/{routeParam1}/{routeParam2}.
To generate complete links that are "routing aware" you need to use MVC LinkGenerator to generate the url of the Get endpoint of the correct controller, in the current http context.
I will paste the code I used below. Any thoughts on the approach? Is there any interest in a PR to either change the current link builder, or add an option to use this version? And if not no worries, folks can easily steal my class below and apply it using the dependency injection framework with services.AddScoped<ILinkBuilder, RoutingLinkBuilder>();
(The main change is adding this method, and calling it everywhere to generate a link to the /{resource}/{id?} endpoint)
privatestringGetResourceLink(ResourceContextresourceContext,stringresource,stringresourceId=null){// if current request has any route params specified we need to fetch them here to reuse them,// and append/set the id of the resource we are routing tovarrouteData=newRouteValueDictionary(_httpContextAccessor.HttpContext.Request.RouteValues){["id"]=resourceId};varcontrollerName=routeData["controller"]?.ToString()??resource;varlink=_linkGenerator.GetPathByAction(_httpContextAccessor.HttpContext,action:GetResourceByIdActionName,controllerName,routeData);returnlink;}
The text was updated successfully, but these errors were encountered:
This looks like a great improvement to me. I'd welcome a PR that replaces the existing logic, so it can be run against our test suite. @maurei thoughts?
DESCRIPTION
Following on from the useful discussion in PR #949 I have implemented a custom ILinkBuilder that uses the MVC routing data to help generate links rather than just string building from static config.
Use cases where this is required:
app.UsePathBase(new PathString("/api"))
If the path base is a fixed string then json api users must also configure the
JsonApiOptions.Namespace
option to prefix all links, but with the route aware link builder, this option is no longer required and all links will include path base by defaultGET /api/{tenantId}/books
. The current LinkBuilder cannot see/understand the path before the resourceName, so all links start/books
rather than the complete link. I have also used other weird routing setups that mess up the links such as/{controller}/{routeParam1}/{routeParam2}
.To generate complete links that are "routing aware" you need to use MVC LinkGenerator to generate the url of the Get endpoint of the correct controller, in the current http context.
I will paste the code I used below. Any thoughts on the approach? Is there any interest in a PR to either change the current link builder, or add an option to use this version? And if not no worries, folks can easily steal my class below and apply it using the dependency injection framework with
services.AddScoped<ILinkBuilder, RoutingLinkBuilder>();
RoutingLinkBuilder.cs gist
(The main change is adding this method, and calling it everywhere to generate a link to the
/{resource}/{id?}
endpoint)The text was updated successfully, but these errors were encountered: