Skip to content

Rewrite of LinkBuilder to use ASP.NET Core routing to render links #987

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

Merged
merged 2 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/usage/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ GET /orderLines HTTP/1.1
It is possible to bypass the default routing convention for a controller.

```c#
[Route("v1/custom/route/orderLines"), DisableRoutingConvention]
[Route("v1/custom/route/lines-in-order"), DisableRoutingConvention]
public class OrderLineController : JsonApiController<OrderLine>
{
public OrderLineController(IJsonApiOptions options, ILoggerFactory loggerFactory,
Expand All @@ -73,8 +73,6 @@ public class OrderLineController : JsonApiController<OrderLine>
}
```

It is required to match your custom url with the exposed name of the associated resource.

## Advanced Usage: Custom Routing Convention

It is possible to replace the built-in routing convention with a [custom routing convention](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/application-model?view=aspnetcore-3.1#sample-custom-routing-convention) by registering an implementation of `IJsonApiRoutingConvention`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ namespace JsonApiDotNetCore.Middleware
public interface IControllerResourceMapping
{
/// <summary>
/// Get the associated resource type for the provided controller type.
/// Gets the associated resource type for the provided controller type.
/// </summary>
Type GetResourceTypeForController(Type controllerType);

/// <summary>
/// Gets the associated controller name for the provided resource type.
/// </summary>
string GetControllerNameForResourceType(Type resourceType);
}
}
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore/Middleware/IJsonApiRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources.Annotations;

Expand All @@ -22,6 +23,7 @@ public interface IJsonApiRequest
/// Relative: /api/v1
/// ]]></code>
/// </example>
[Obsolete("This value is calculated for backwards compatibility, but it is no longer used and will be removed in a future version.")]
string BasePath { get; }

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore/Middleware/JsonApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public void CopyFrom(IJsonApiRequest other)
ArgumentGuard.NotNull(other, nameof(other));

Kind = other.Kind;
#pragma warning disable CS0618 // Type or member is obsolete
BasePath = other.BasePath;
#pragma warning restore CS0618 // Type or member is obsolete
PrimaryId = other.PrimaryId;
PrimaryResource = other.PrimaryResource;
SecondaryResource = other.SecondaryResource;
Expand Down
26 changes: 20 additions & 6 deletions src/JsonApiDotNetCore/Middleware/JsonApiRoutingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class JsonApiRoutingConvention : IJsonApiRoutingConvention
private readonly IResourceContextProvider _resourceContextProvider;
private readonly Dictionary<string, string> _registeredControllerNameByTemplate = new Dictionary<string, string>();
private readonly Dictionary<Type, ResourceContext> _resourceContextPerControllerTypeMap = new Dictionary<Type, ResourceContext>();
private readonly Dictionary<ResourceContext, ControllerModel> _controllerPerResourceContextMap = new Dictionary<ResourceContext, ControllerModel>();

public JsonApiRoutingConvention(IJsonApiOptions options, IResourceContextProvider resourceContextProvider)
{
Expand All @@ -58,6 +59,22 @@ public Type GetResourceTypeForController(Type controllerType)
return null;
}

/// <inheritdoc />
public string GetControllerNameForResourceType(Type resourceType)
{
ArgumentGuard.NotNull(resourceType, nameof(resourceType));

ResourceContext resourceContext = _resourceContextProvider.GetResourceContext(resourceType);

if (_controllerPerResourceContextMap.TryGetValue(resourceContext, out ControllerModel controllerModel))

{
return controllerModel.ControllerName;
}

return null;
}

/// <inheritdoc />
public void Apply(ApplicationModel application)
{
Expand All @@ -78,6 +95,7 @@ public void Apply(ApplicationModel application)
if (resourceContext != null)
{
_resourceContextPerControllerTypeMap.Add(controller.ControllerType, resourceContext);
_controllerPerResourceContextMap.Add(resourceContext, controller);
}
}
}
Expand Down Expand Up @@ -117,9 +135,7 @@ private string TemplateFromResource(ControllerModel model)
{
if (_resourceContextPerControllerTypeMap.TryGetValue(model.ControllerType, out ResourceContext resourceContext))
{
string template = $"{_options.Namespace}/{resourceContext.PublicName}";

return template;
return $"{_options.Namespace}/{resourceContext.PublicName}";
}

return null;
Expand All @@ -131,9 +147,7 @@ private string TemplateFromResource(ControllerModel model)
private string TemplateFromController(ControllerModel model)
{
string controllerName = _options.SerializerNamingStrategy.GetPropertyName(model.ControllerName, false);
string template = $"{_options.Namespace}/{controllerName}";

return template;
return $"{_options.Namespace}/{controllerName}";
}

/// <summary>
Expand Down
Loading