Skip to content

Feat/allow non dasherized routes #88

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 4 commits into from
Apr 9, 2017
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);
}
}
}