mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
Changing how the UrlHelper is registered in IoC
Added delegates to the MvcModule to resolve context-specific components --HG-- branch : dev
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
using System.Web.Routing;
|
||||||
|
using Autofac;
|
||||||
using Autofac.Builder;
|
using Autofac.Builder;
|
||||||
using Autofac.Integration.Web.Mvc;
|
using Autofac.Integration.Web.Mvc;
|
||||||
using Orchard.Controllers;
|
using Orchard.Controllers;
|
||||||
@@ -9,11 +12,9 @@ using Orchard.Extensions;
|
|||||||
|
|
||||||
namespace Orchard.Mvc {
|
namespace Orchard.Mvc {
|
||||||
public class MvcModule : Module {
|
public class MvcModule : Module {
|
||||||
private readonly ICompositionStrategy _compositionStrategy;
|
|
||||||
private readonly IExtensionManager _extensionManager;
|
private readonly IExtensionManager _extensionManager;
|
||||||
|
|
||||||
public MvcModule(ICompositionStrategy compositionStrategy, IExtensionManager extensionManager) {
|
public MvcModule(IExtensionManager extensionManager) {
|
||||||
_compositionStrategy = compositionStrategy;
|
|
||||||
_extensionManager = extensionManager;
|
_extensionManager = extensionManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,10 +28,31 @@ namespace Orchard.Mvc {
|
|||||||
};
|
};
|
||||||
|
|
||||||
moduleBuilder.RegisterModule(module);
|
moduleBuilder.RegisterModule(module);
|
||||||
moduleBuilder
|
moduleBuilder.Register(ctx => HttpContextBaseFactory(ctx)).As<HttpContextBase>().FactoryScoped();
|
||||||
.Register(ctx => HttpContext.Current == null ? (HttpContextBase)new HttpContextPlaceholder() : new HttpContextWrapper(HttpContext.Current))
|
moduleBuilder.Register(ctx => RequestContextFactory(ctx)).As<RequestContext>().FactoryScoped();
|
||||||
.As<HttpContextBase>()
|
moduleBuilder.Register(ctx => UrlHelperFactory(ctx)).As<UrlHelper>().FactoryScoped();
|
||||||
.FactoryScoped();
|
}
|
||||||
|
|
||||||
|
static HttpContextBase HttpContextBaseFactory(IContext context) {
|
||||||
|
if (HttpContext.Current != null) {
|
||||||
|
return new HttpContextWrapper(HttpContext.Current);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpContextPlaceholder();
|
||||||
|
}
|
||||||
|
|
||||||
|
static RequestContext RequestContextFactory(IContext context) {
|
||||||
|
var httpContext = context.Resolve<HttpContextBase>();
|
||||||
|
var mvcHandler = httpContext.Handler as MvcHandler;
|
||||||
|
if (mvcHandler != null) {
|
||||||
|
return mvcHandler.RequestContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new RequestContext(httpContext, new RouteData());
|
||||||
|
}
|
||||||
|
|
||||||
|
static UrlHelper UrlHelperFactory(IContext context) {
|
||||||
|
return new UrlHelper(context.Resolve<RequestContext>(), context.Resolve<RouteCollection>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@@ -12,8 +12,6 @@ namespace Orchard.Mvc {
|
|||||||
// Locate the container this route is bound against
|
// Locate the container this route is bound against
|
||||||
var container = GetRequestContainer(routeData);
|
var container = GetRequestContainer(routeData);
|
||||||
|
|
||||||
container.Build(cb => cb.Register(new UrlHelper(requestContext)));
|
|
||||||
|
|
||||||
// Determine the area name for the request, and fall back to stock orchard controllers
|
// Determine the area name for the request, and fall back to stock orchard controllers
|
||||||
var areaName = GetAreaName(routeData) ?? "Orchard";
|
var areaName = GetAreaName(routeData) ?? "Orchard";
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user