Asp.net mvc 3 定制网页ZorhostFactory
就像我可以通过调用Asp.net mvc 3 定制网页ZorhostFactory,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,就像我可以通过调用ControllerBuilder.Current.SetControllerFactory()在global.asax中将DefaultControllerFactory设置为我自己的工厂一样,并提供工厂的自定义实现,是否有可能以某种方式为应用程序提供我自己的Web GerazorHostFactory的实现? 我想要实现的是将视图封装在IoC容器中,以提供依赖注入。我已经阅读了以下关于Haacked的文章:我认为这可能会增加解决方案 以下操作似乎不起作用,并引发错误: 无法
ControllerBuilder.Current.SetControllerFactory()
在global.asax
中将DefaultControllerFactory设置为我自己的工厂一样,并提供工厂的自定义实现,是否有可能以某种方式为应用程序提供我自己的Web GerazorHostFactory的实现?
我想要实现的是将视图封装在IoC容器中,以提供依赖注入。我已经阅读了以下关于Haacked的文章:我认为这可能会增加解决方案
以下操作似乎不起作用,并引发错误:
无法将“ASP.\U页面\视图\视图开始\ cshtml”类型的对象强制转换为“System.Web.WebPages.StartPage”
Global.asax:(C#project)
类定义:(VB项目)
);在Web.config中:
<system.web.webPages.razor>
<host factoryType="MyProject.MyWebPageRazorHost" />
<pages pageBaseType="MyProject.CustomWebPage">
<namespaces>
<add namespace="MyProject.SomeNamespace" />
</namespaces>
</pages>
</system.web.webPages.razor>
; 这将大有帮助。
查看mvc3 rtm sources\webpages\src\System.Web.webpages.Razor
我的意思是在global.asax
中设置它,这样我就可以提供Windsor(我正在使用的IoC框架)的内核了,这是一个好问题;让我调查一下。至少,您可以制作一个包装器,从IoC创建一个内部实例。。。我收到以下错误:无法将类型为“ASP.\u Page\u Views\u ViewStart\u cshtml”的对象强制转换为类型为“System.Web.WebPages.StartPage”。
请查看我附加的源代码…@Ropstah:将您的代码与源代码进行比较;看看它是如何编译StartPage的。
Public Class DefaultRazorBuildProvider : Inherits System.Web.WebPages.Razor.RazorBuildProvider
Private Shared _factories As New ConcurrentDictionary(Of String, Func(Of IKernel, DefaultWebPageRazorHostFactory))(StringComparer.OrdinalIgnoreCase)
Private Shared _kernel As IKernel
Friend Shared TypeFactory As Func(Of String, Type) = AddressOf DefaultTypeFactory
Public Shared Function GetKernel() As IKernel
If _kernel Is Nothing Then Throw New NullReferenceException("_kernel is not set")
Return _kernel
End Function
Public Shared Sub SetKernel(ByVal kernel As IKernel)
_kernel = kernel
End Sub
Public Shared Function CreateHostFromConfig(ByVal virtualPath As String) As WebPageRazorHost
Return CreateHostFromConfig(virtualPath, Nothing)
End Function
Public Shared Function CreateHostFromConfig(ByVal virtualPath As String, ByVal physicalPath As String) As WebPageRazorHost
If [String].IsNullOrEmpty(virtualPath) Then
Throw New ArgumentNullException("virtualPath")
End If
Return CreateHostFromConfigCore(GetRazorSection(virtualPath), virtualPath, physicalPath)
End Function
Public Shared Function CreateHostFromConfig(ByVal config As RazorWebSectionGroup, ByVal virtualPath As String) As WebPageRazorHost
Return CreateHostFromConfig(config, virtualPath, Nothing)
End Function
Public Shared Function CreateHostFromConfig(ByVal config As RazorWebSectionGroup, ByVal virtualPath As String, ByVal physicalPath As String) As WebPageRazorHost
If config Is Nothing Then
Throw New ArgumentNullException("config")
End If
If [String].IsNullOrEmpty(virtualPath) Then
Throw New ArgumentNullException("virtualPath")
End If
Return CreateHostFromConfigCore(config, virtualPath, physicalPath)
End Function
Friend Shared Function CreateHostFromConfigCore(ByVal config As RazorWebSectionGroup, ByVal virtualPath As String) As WebPageRazorHost
Return CreateHostFromConfigCore(config, virtualPath, Nothing)
End Function
Private Shared Function CreateFactory(ByVal typeName As String) As Func(Of IKernel, DefaultWebPageRazorHostFactory)
Dim factoryType As Type = TypeFactory(typeName)
If factoryType Is Nothing Then
Throw New InvalidOperationException("Factory type not valid")
End If
Dim param = Expression.Parameter(GetType(IKernel))
Dim body = Expression.[New](factoryType.GetConstructor(New Type() {GetType(IKernel)}), param)
Return Expression.Lambda(Of Func(Of IKernel, DefaultWebPageRazorHostFactory))(body, param).Compile()
End Function
Public Shared Sub ApplyConfigurationToHost(ByVal config As RazorPagesSection, ByVal host As WebPageRazorHost)
host.DefaultPageBaseClass = config.PageBaseType
For Each import As String In config.Namespaces.OfType(Of NamespaceInfo)().[Select](Function(ns) ns.[Namespace])
host.NamespaceImports.Add(import)
Next
End Sub
Friend Shared Function CreateHostFromConfigCore(ByVal config As RazorWebSectionGroup, ByVal virtualPath As String, ByVal physicalPath As String) As WebPageRazorHost
virtualPath = EnsureAppRelative(virtualPath)
If virtualPath.StartsWith("~/App_Code", StringComparison.OrdinalIgnoreCase) Then
Return New WebCodeRazorHost(virtualPath, physicalPath)
End If
Dim factory As DefaultWebPageRazorHostFactory = Nothing
If config IsNot Nothing AndAlso config.Host IsNot Nothing AndAlso Not [String].IsNullOrEmpty(config.Host.FactoryType) Then
Dim factoryCreator As Func(Of IKernel, DefaultWebPageRazorHostFactory) = _factories.GetOrAdd(config.Host.FactoryType, CreateFactory(config.Type))
Debug.Assert(factoryCreator IsNot Nothing)
factory = factoryCreator(_kernel)
End If
Dim host As WebPageRazorHost = (If(factory, New DefaultWebPageRazorHostFactory(_kernel))).CreateHost(virtualPath, physicalPath)
If config IsNot Nothing AndAlso config.Pages IsNot Nothing Then
ApplyConfigurationToHost(config.Pages, host)
End If
Return host
End Function
Friend Shared Function GetRazorSection(ByVal virtualPath As String) As RazorWebSectionGroup
Return New RazorWebSectionGroup() With { _
.Host = DirectCast(WebConfigurationManager.GetSection(HostSection.SectionName, virtualPath), HostSection), _
.Pages = DirectCast(WebConfigurationManager.GetSection(RazorPagesSection.SectionName, virtualPath), RazorPagesSection) _
}
End Function
Private Shared Function EnsureAppRelative(ByVal virtualPath As String) As String
If HostingEnvironment.IsHosted Then
virtualPath = VirtualPathUtility.ToAppRelative(virtualPath)
Else
If virtualPath.StartsWith("/", StringComparison.Ordinal) Then
virtualPath = "~" & virtualPath
ElseIf Not virtualPath.StartsWith("~/", StringComparison.Ordinal) Then
virtualPath = "~/" & virtualPath
End If
End If
Return virtualPath
End Function
Private Shared Function DefaultTypeFactory(ByVal typeName As String) As Type
Return BuildManager.[GetType](typeName, False, False)
End Function
End Class
Public Class DefaultWebPageRazorHostFactory : Inherits System.Web.WebPages.Razor.WebRazorHostFactory
Private _kernel As IKernel
Public Sub New()
Me._kernel = Nothing
End Sub
Public Sub New(ByVal kernel As IKernel)
Me._kernel = kernel
End Sub
Public Overrides Function CreateHost(ByVal virtualPath As String, ByVal physicalPath As String) As System.Web.WebPages.Razor.WebPageRazorHost
Return New DefaultWebPageRazorHost(Me._kernel, virtualPath, physicalPath)
End Function
End Class
Public Class DefaultWebPageRazorHost : Inherits System.Web.WebPages.Razor.WebPageRazorHost
Private _kernel As IKernel
Sub New(ByVal kernel As IKernel, ByVal virtualPath As String, ByVal physicalPath As String)
MyBase.New(virtualPath, physicalPath)
Me._kernel = kernel
End Sub
End Class
<system.web.webPages.razor>
<host factoryType="MyProject.MyWebPageRazorHost" />
<pages pageBaseType="MyProject.CustomWebPage">
<namespaces>
<add namespace="MyProject.SomeNamespace" />
</namespaces>
</pages>
</system.web.webPages.razor>