Skip to content

Commit c4b3361

Browse files
authored
Merge pull request #88 from Research-Institute/feat/allow-non-dasherized-routes
Feat/allow non dasherized routes
2 parents 53cc9db + 64c9723 commit c4b3361

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// REF: https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.CustomRoutingConvention/NameSpaceRoutingConvention.cs
22
// REF: https://github.com/aspnet/Mvc/issues/5691
3+
using JsonApiDotNetCore.Controllers;
34
using JsonApiDotNetCore.Extensions;
45
using Microsoft.AspNetCore.Mvc.ApplicationModels;
56

@@ -12,17 +13,28 @@ public DasherizedRoutingConvention(string nspace)
1213
{
1314
_namespace = nspace;
1415
}
15-
16+
1617
public void Apply(ApplicationModel application)
1718
{
1819
foreach (var controller in application.Controllers)
19-
{
20-
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
21-
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
20+
{
21+
if (IsJsonApiController(controller))
2222
{
23-
Template = template
24-
};
23+
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
24+
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
25+
{
26+
Template = template
27+
};
28+
}
2529
}
2630
}
31+
32+
private bool IsJsonApiController(ControllerModel controller)
33+
{
34+
var controllerBaseType = controller.ControllerType.BaseType;
35+
if(!controllerBaseType.IsConstructedGenericType) return false;
36+
var genericTypeDefinition = controllerBaseType.GetGenericTypeDefinition();
37+
return (genericTypeDefinition == typeof(JsonApiController<,>) || genericTypeDefinition == typeof(JsonApiController<>));
38+
}
2739
}
2840
}

src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<VersionPrefix>1.3.0</VersionPrefix>
3+
<VersionPrefix>1.3.1</VersionPrefix>
44
<TargetFramework>netcoreapp1.0</TargetFramework>
55
<AssemblyName>JsonApiDotNetCore</AssemblyName>
66
<PackageId>JsonApiDotNetCore</PackageId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace JsonApiDotNetCoreExample.Controllers
4+
{
5+
[Route("[controller]")]
6+
public class TestValuesController : Controller
7+
{
8+
[HttpGet]
9+
public IActionResult Get()
10+
{
11+
var result = new string[] { "value" };
12+
return Ok(result);
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.TestHost;
6+
using Xunit;
7+
using JsonApiDotNetCoreExample;
8+
9+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
10+
{
11+
[Collection("WebHostCollection")]
12+
public class CustomControllerTests
13+
{
14+
[Fact]
15+
public async Task NonJsonApiControllers_DoNotUse_Dasherized_Routes()
16+
{
17+
// arrange
18+
var builder = new WebHostBuilder()
19+
.UseStartup<Startup>();
20+
var httpMethod = new HttpMethod("GET");
21+
var route = $"testValues";
22+
23+
var server = new TestServer(builder);
24+
var client = server.CreateClient();
25+
var request = new HttpRequestMessage(httpMethod, route);
26+
27+
// act
28+
var response = await client.SendAsync(request);
29+
30+
// assert
31+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)