Skip to content
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
24 changes: 18 additions & 6 deletions src/JsonApiDotNetCore/Internal/DasherizedRoutingConvention.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// REF: https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.CustomRoutingConvention/NameSpaceRoutingConvention.cs
// REF: https://github.com/aspnet/Mvc/issues/5691
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Extensions;
using Microsoft.AspNetCore.Mvc.ApplicationModels;

Expand All @@ -12,17 +13,28 @@ public DasherizedRoutingConvention(string nspace)
{
_namespace = nspace;
}

public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
{
if (IsJsonApiController(controller))
{
Template = template
};
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
{
Template = template
};
}
}
}

private bool IsJsonApiController(ControllerModel controller)
{
var controllerBaseType = controller.ControllerType.BaseType;
if(!controllerBaseType.IsConstructedGenericType) return false;
var genericTypeDefinition = controllerBaseType.GetGenericTypeDefinition();
return (genericTypeDefinition == typeof(JsonApiController<,>) || genericTypeDefinition == typeof(JsonApiController<>));
}
}
}
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>1.3.0</VersionPrefix>
<VersionPrefix>1.3.1</VersionPrefix>
<TargetFramework>netcoreapp1.0</TargetFramework>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down
15 changes: 15 additions & 0 deletions src/JsonApiDotNetCoreExample/Controllers/TestValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Mvc;

namespace JsonApiDotNetCoreExample.Controllers
{
[Route("[controller]")]
public class TestValuesController : Controller
{
[HttpGet]
public IActionResult Get()
{
var result = new string[] { "value" };
return Ok(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Xunit;
using JsonApiDotNetCoreExample;

namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
{
[Collection("WebHostCollection")]
public class CustomControllerTests
{
[Fact]
public async Task NonJsonApiControllers_DoNotUse_Dasherized_Routes()
{
// arrange
var builder = new WebHostBuilder()
.UseStartup<Startup>();
var httpMethod = new HttpMethod("GET");
var route = $"testValues";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
}