namespaced-http-controller-selector
1.0.0
dotnet add package namespaced-http-controller-selector --version 1.0.0
NuGet\Install-Package namespaced-http-controller-selector -Version 1.0.0
<PackageReference Include="namespaced-http-controller-selector" Version="1.0.0" />
paket add namespaced-http-controller-selector --version 1.0.0
#r "nuget: namespaced-http-controller-selector, 1.0.0"
// Install namespaced-http-controller-selector as a Cake Addin #addin nuget:?package=namespaced-http-controller-selector&version=1.0.0 // Install namespaced-http-controller-selector as a Cake Tool #tool nuget:?package=namespaced-http-controller-selector&version=1.0.0
Namespaced HttpController
Since Areas are not officially supported for WebAPI, you can now use this package to define namespace parameter when mapping routes similar to the existing way in the RegisterRoutes method, because the way in which Web API looks for a controller class that inherits ApiController doesn't takes into account the controller's namespace.
When you simply create Controllers with duplicate names in different Areas (like versioning an API, v1 and v2), you'll get an error like:
Multiple types were found that match the controller named 'User'. This can happen if the route that services this request ('api/v1/{controller}/{action}/{id}') found multiple controllers defined with the same name but differing namespaces, which is not supported. The request for 'User' has found the following matching controllers: Base.V1.UserController Base.V2.UserController
How to use
You must either add a NuGet reference to Microsoft.AspNet.WebApi.Core or enable WebAPI when starting your project.
Open your WebApiConfig.cs and inside the Register method, add:
config.Services.Replace(
typeof(System.Web.Http.Dispatcher.IHttpControllerSelector),
new NamespacedHttpController.NamespaceHttpControllerSelector(config));
Add namespaces
to the defaults parameter like:
private const string NAMESPACE_EXAMPLE = "com.Project.Web.Areas.Api";
config.Routes.MapHttpRoute(
name: "ExampleApi",
routeTemplate: "areaName/api/{controller}/{action}",
defaults: new { namespaces = new[] { NAMESPACE_EXAMPLE } }
);
Now you're going to be able to match .../area1/api/user/list
, .../api/user/list
and .../area2/api/user/list
simultaneously, creating UserController : ApiController
classes in different namespaces.
Learn more about Target Frameworks and .NET Standard.
-
- Microsoft.AspNet.WebApi.Client (>= 5.2.7)
- Microsoft.AspNet.WebApi.Core (>= 5.2.7)
- System.Web.Http.Common (>= 4.0.20126.16343)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 2,576 | 5/3/2019 |
Initial Release