Fixing WebApi integration for RTM

--HG--
branch : 1.x
extra : transplant_source : %93%0A%C5%5Cd%BA%02%04%C8V%90%ACV%FDa%ED2Q%25%98
This commit is contained in:
Sebastien Ros
2012-08-13 18:03:29 -07:00
parent 156f218501
commit c0322db45f
5 changed files with 30 additions and 22 deletions

View File

@@ -6,4 +6,10 @@ namespace Orchard.Mvc.Routes {
public int Priority { get; set; }
public RouteBase Route { get; set; }
}
public class HttpRouteDescriptor : RouteDescriptor {
public string RouteTemplate { get; set; }
public object Defaults { get; set; }
public object Constraints { get; set; }
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Routing;
using Orchard.Environment;
using Orchard.Environment.Configuration;
@@ -30,8 +31,19 @@ namespace Orchard.Mvc.Routes {
// this is not called often, but is intended to surface problems before
// the actual collection is modified
var preloading = new RouteCollection();
foreach (var route in routesArray)
preloading.Add(route.Name, route.Route);
foreach (var routeDescriptor in routesArray) {
// extract the WebApi route implementation
var httpRouteDescriptor = routeDescriptor as HttpRouteDescriptor;
if (httpRouteDescriptor != null) {
var httpRouteCollection = new RouteCollection();
httpRouteCollection.MapHttpRoute(httpRouteDescriptor.Name, httpRouteDescriptor.RouteTemplate, httpRouteDescriptor.Defaults);
routeDescriptor.Route = httpRouteCollection.First();
}
preloading.Add(routeDescriptor.Name, routeDescriptor.Route);
}
using (_routeCollection.GetWriteLock()) {
// existing routes are removed while the collection is briefly inaccessable
@@ -46,7 +58,7 @@ namespace Orchard.Mvc.Routes {
// new routes are added
foreach (var routeDescriptor in routesArray) {
var shellRoute = new ShellRoute(routeDescriptor.Route, _shellSettings, _workContextAccessor, _runningShellTable);
var shellRoute = new ShellRoute(routeDescriptor.Route, _shellSettings, _workContextAccessor, _runningShellTable){IsHttpRoute = routeDescriptor is HttpRouteDescriptor};
_routeCollection.Add(routeDescriptor.Name, shellRoute);
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Web;
using System.Web.Http.WebHost;
using System.Web.Http.WebHost.Routing;
using System.Web.Mvc;
using System.Web.Routing;
@@ -31,6 +32,7 @@ namespace Orchard.Mvc.Routes {
public string ShellSettingsName { get { return _shellSettings.Name; } }
public string Area { get; private set; }
public bool IsHttpRoute { get; set; }
public override RouteData GetRouteData(HttpContextBase httpContext) {
// locate appropriate shell settings for request
@@ -52,7 +54,7 @@ namespace Orchard.Mvc.Routes {
routeData.RouteHandler = new RouteHandler(_workContextAccessor, routeData.RouteHandler);
routeData.DataTokens["IWorkContextAccessor"] = _workContextAccessor;
if (_route is HttpWebRoute) {
if (IsHttpRoute) {
routeData.Values["IWorkContextAccessor"] = _workContextAccessor; // for WebApi
}
@@ -93,6 +95,7 @@ namespace Orchard.Mvc.Routes {
public IHttpHandler GetHttpHandler(RequestContext requestContext) {
var httpHandler = _routeHandler.GetHttpHandler(requestContext);
if (httpHandler is IHttpAsyncHandler) {
return new HttpAsyncHandler(_workContextAccessor, (IHttpAsyncHandler)httpHandler);
}

View File

@@ -24,7 +24,7 @@ namespace Orchard.WebApi.Extensions {
public static string GetAreaName(this IHttpRouteData routeData) {
object area;
if (routeData.Route.DataTokens.TryGetValue("area", out area)) {
if (routeData.Route.Defaults.TryGetValue("area", out area)) {
return area as string;
}

View File

@@ -1,9 +1,6 @@
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 System.Web.Http;
using Orchard.Environment.ShellBuilders.Models;
using Orchard.Mvc.Routes;
@@ -24,20 +21,10 @@ namespace Orchard.WebApi.Routes {
var areaName = item.Key;
var displayPath = item.Distinct().Single();
yield return new RouteDescriptor {
yield return new HttpRouteDescriptor {
Priority = -10,
Route = new HttpWebRoute(
"api/" + displayPath + "/{controller}/{id}",
new RouteValueDictionary {
{"area", areaName},
{"controller", "api"},
{"id", ""}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", areaName}
},
HttpControllerRouteHandler.Instance)
RouteTemplate = "api/" + displayPath + "/{controller}/{id}",
Defaults = new {area = areaName, controller = "api", id = RouteParameter.Optional}
};
}
}