API Controllers Factory whit dependency injection
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
namespace LaaProductionWeb.App_Infrastructure
|
||||
{
|
||||
using LaaProductionWeb.Rest;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.Dispatcher;
|
||||
|
||||
public class APIControllerActivator : IHttpControllerActivator
|
||||
{
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public APIControllerActivator(IServiceProvider serviceProvider)
|
||||
=> this.serviceProvider = serviceProvider;
|
||||
|
||||
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
|
||||
=> this.serviceProvider.GetService(controllerType) as IHttpController;
|
||||
}
|
||||
|
||||
public class APIControllerFactory : IHttpControllerSelector
|
||||
{
|
||||
public HttpControllerDescriptor SelectController(HttpRequestMessage request)
|
||||
{
|
||||
var routeData = request.GetRouteData();
|
||||
var controllerName = default(string);
|
||||
var controllerType = default(Type);
|
||||
|
||||
if (routeData.Values.TryGetValue("controller", out object controllerValue))
|
||||
{
|
||||
controllerName = $"{controllerValue}Controller";
|
||||
}
|
||||
|
||||
if (routeData.Values.TryGetValue("area", out object area))
|
||||
{
|
||||
controllerType = Type.GetType($"LaaProductionWeb.Areas.{area}.Rest.{controllerName}", false, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
controllerType = Type.GetType($"LaaProductionWeb.Rest.{controllerName}", false, true);
|
||||
}
|
||||
|
||||
return new HttpControllerDescriptor()
|
||||
{
|
||||
ControllerName = controllerName,
|
||||
ControllerType = controllerType,
|
||||
Configuration = GlobalConfiguration.Configuration
|
||||
};
|
||||
}
|
||||
|
||||
public IDictionary<string, HttpControllerDescriptor> GetControllerMapping()
|
||||
=> new Dictionary<string, HttpControllerDescriptor>();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace LaaProductionWeb.App_Infrastructure
|
||||
{
|
||||
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
using System.Reflection;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Principal;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.Dispatcher;
|
||||
using System.Web.Mvc;
|
||||
|
||||
public static class ControllersExtensions
|
||||
@@ -28,12 +31,29 @@
|
||||
|
||||
return services;
|
||||
}
|
||||
public static IServiceCollection AddAPIControllers(this IServiceCollection services)
|
||||
{
|
||||
var controllers = typeof(ControllersExtensions)
|
||||
.Assembly
|
||||
.GetTypes()
|
||||
.Where(x => !x.IsAbstract
|
||||
&& typeof(IHttpController).IsAssignableFrom(x));
|
||||
|
||||
foreach (var controller in controllers)
|
||||
{
|
||||
services.AddTransient(controller);
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceProvider BuildControllerFactory(this IServiceProvider serviceProvider)
|
||||
{
|
||||
var controllerFactory = new ControllerFactory(serviceProvider);
|
||||
|
||||
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
|
||||
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new APIControllerActivator(serviceProvider));
|
||||
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new APIControllerFactory());
|
||||
|
||||
return serviceProvider;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
{
|
||||
services
|
||||
.AddControllers()
|
||||
.AddAPIControllers()
|
||||
.AddTransientServices()
|
||||
.AddDbContext(ApplicationSettings.ConnectionString);
|
||||
|
||||
|
||||
@@ -150,6 +150,7 @@
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="App_Infrastructure\APIControllerFactory.cs" />
|
||||
<Compile Include="App_Infrastructure\ApplicationSettings.cs" />
|
||||
<Compile Include="App_Infrastructure\AuthorizationFilter.cs" />
|
||||
<Compile Include="App_Infrastructure\ControllerFactory.cs" />
|
||||
@@ -175,6 +176,7 @@
|
||||
<DependentUpon>Global.asax</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Rest\RestController.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="App_Content\css\bootstrap.min.css" />
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace LaaProductionWeb.Rest
|
||||
{
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Results;
|
||||
|
||||
public class RestController : ApiController
|
||||
{
|
||||
public JsonResult<string> Get()
|
||||
{
|
||||
return this.Json("it works");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user