mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Fixing Web API routes resolution
--HG-- branch : 1.x
This commit is contained in:
@@ -6,12 +6,14 @@ using Orchard.Logging;
|
||||
using Orchard.Mvc.ModelBinders;
|
||||
using Orchard.Mvc.Routes;
|
||||
using Orchard.Tasks;
|
||||
using Orchard.WebApi.Routes;
|
||||
using IModelBinderProvider = Orchard.Mvc.ModelBinders.IModelBinderProvider;
|
||||
|
||||
namespace Orchard.Environment {
|
||||
public class DefaultOrchardShell : IOrchardShell {
|
||||
private readonly Func<Owned<IOrchardShellEvents>> _eventsFactory;
|
||||
private readonly IEnumerable<IRouteProvider> _routeProviders;
|
||||
private readonly IEnumerable<IHttpRouteProvider> _httpRouteProviders;
|
||||
private readonly IRoutePublisher _routePublisher;
|
||||
private readonly IEnumerable<IModelBinderProvider> _modelBinderProviders;
|
||||
private readonly IModelBinderPublisher _modelBinderPublisher;
|
||||
@@ -20,12 +22,14 @@ namespace Orchard.Environment {
|
||||
public DefaultOrchardShell(
|
||||
Func<Owned<IOrchardShellEvents>> eventsFactory,
|
||||
IEnumerable<IRouteProvider> routeProviders,
|
||||
IEnumerable<IHttpRouteProvider> httpRouteProviders,
|
||||
IRoutePublisher routePublisher,
|
||||
IEnumerable<IModelBinderProvider> modelBinderProviders,
|
||||
IModelBinderPublisher modelBinderPublisher,
|
||||
ISweepGenerator sweepGenerator) {
|
||||
_eventsFactory = eventsFactory;
|
||||
_routeProviders = routeProviders;
|
||||
_httpRouteProviders = httpRouteProviders;
|
||||
_routePublisher = routePublisher;
|
||||
_modelBinderProviders = modelBinderProviders;
|
||||
_modelBinderPublisher = modelBinderPublisher;
|
||||
@@ -37,7 +41,11 @@ namespace Orchard.Environment {
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public void Activate() {
|
||||
_routePublisher.Publish(_routeProviders.SelectMany(provider => provider.GetRoutes()));
|
||||
var allRoutes = new List<RouteDescriptor>();
|
||||
allRoutes.AddRange(_routeProviders.SelectMany(provider => provider.GetRoutes()));
|
||||
allRoutes.AddRange(_httpRouteProviders.SelectMany(provider => provider.GetRoutes()));
|
||||
|
||||
_routePublisher.Publish(allRoutes);
|
||||
_modelBinderPublisher.Publish(_modelBinderProviders.SelectMany(provider => provider.GetModelBinders()));
|
||||
|
||||
_sweepGenerator.Activate();
|
||||
|
@@ -101,10 +101,12 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
.WithMetadata("ControllerType", item.Type)
|
||||
.InstancePerDependency()
|
||||
.OnActivating(e => {
|
||||
var controller = e.Instance as Controller;
|
||||
if (controller != null)
|
||||
controller.ActionInvoker = (IActionInvoker)e.Context.ResolveService(new TypedService(typeof(IActionInvoker)));
|
||||
});
|
||||
// necessary to inject custom filters dynamically
|
||||
// see FilterResolvingActionInvoker
|
||||
var controller = e.Instance as Controller;
|
||||
if (controller != null)
|
||||
controller.ActionInvoker = (IActionInvoker)e.Context.ResolveService(new TypedService(typeof(IActionInvoker)));
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var item in blueprint.HttpControllers) {
|
||||
|
@@ -49,7 +49,7 @@ namespace Orchard.Mvc.Routes {
|
||||
|
||||
// otherwise wrap handler and return it
|
||||
routeData.RouteHandler = new RouteHandler(_workContextAccessor, routeData.RouteHandler);
|
||||
routeData.Values["IWorkContextAccessor"] = _workContextAccessor; //NGM : Added for WebApi
|
||||
routeData.Values["IWorkContextAccessor"] = _workContextAccessor; // for WebApi
|
||||
routeData.DataTokens["IWorkContextAccessor"] = _workContextAccessor;
|
||||
return routeData;
|
||||
}
|
||||
|
@@ -550,6 +550,8 @@
|
||||
<Compile Include="WebApi\DefaultOrchardWebApiHttpControllerSelector.cs" />
|
||||
<Compile Include="WebApi\DefaultOrchardWebApiHttpHttpControllerActivator.cs" />
|
||||
<Compile Include="WebApi\Extensions\RouteExtension.cs" />
|
||||
<Compile Include="WebApi\Routes\IHttpRouteProvider.cs" />
|
||||
<Compile Include="WebApi\Routes\StandardExtensionHttpRouteProvider.cs" />
|
||||
<Compile Include="WorkContextExtensions.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\Razor\RazorCompilationEventsShim.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\Razor\RazorViewEngineProvider.cs" />
|
||||
|
9
src/Orchard/WebApi/Routes/IHttpRouteProvider.cs
Normal file
9
src/Orchard/WebApi/Routes/IHttpRouteProvider.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Mvc.Routes;
|
||||
|
||||
namespace Orchard.WebApi.Routes {
|
||||
public interface IHttpRouteProvider : IDependency {
|
||||
IEnumerable<RouteDescriptor> GetRoutes();
|
||||
void GetRoutes(ICollection<RouteDescriptor> routes);
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Http.WebHost;
|
||||
using System.Web.Http.WebHost.Routing;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Environment.ShellBuilders.Models;
|
||||
using Orchard.Mvc.Routes;
|
||||
|
||||
namespace Orchard.WebApi.Routes {
|
||||
public class StandardExtensionHttpRouteProvider : IHttpRouteProvider {
|
||||
private readonly ShellBlueprint _blueprint;
|
||||
|
||||
public StandardExtensionHttpRouteProvider(ShellBlueprint blueprint) {
|
||||
_blueprint = blueprint;
|
||||
}
|
||||
|
||||
public IEnumerable<RouteDescriptor> GetRoutes() {
|
||||
var displayPathsPerArea = _blueprint.HttpControllers.GroupBy(
|
||||
x => x.AreaName,
|
||||
x => x.Feature.Descriptor.Extension.Path);
|
||||
|
||||
foreach (var item in displayPathsPerArea) {
|
||||
var areaName = item.Key;
|
||||
var displayPath = item.Distinct().Single();
|
||||
|
||||
yield return new RouteDescriptor {
|
||||
Priority = -10,
|
||||
Route = new HttpWebRoute(
|
||||
"api/" + displayPath + "/{controller}/{id}",
|
||||
new RouteValueDictionary {
|
||||
{"area", areaName},
|
||||
{"controller", "api"},
|
||||
{"id", ""}
|
||||
},
|
||||
new RouteValueDictionary(),
|
||||
new RouteValueDictionary {
|
||||
{"area", areaName}
|
||||
},
|
||||
HttpControllerRouteHandler.Instance)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void GetRoutes(ICollection<RouteDescriptor> routes) {
|
||||
foreach (var routeDescriptor in GetRoutes())
|
||||
routes.Add(routeDescriptor);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user