Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3989844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:25:23+00:00 2026-05-20T06:25:23+00:00

Just like I can set the DefaultControllerFactory to my own in the global.asax by

  • 0

Just like I can set the DefaultControllerFactory to my own in the global.asax by calling ControllerBuilder.Current.SetControllerFactory() and provide a custom implementation of the factory, is it possible to somehow provide the application with my own implementation of the WebPageRazorHostFactory?

What I want to achieve is encapsulating the views in a IoC container to provide dependency injection. I’ve read the following article on Haacked: http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx and i think this might add to that solution.

The following doesn’t seem to work and throws an error:

Unable to cast object of type ‘ASP._Page_Views__ViewStart_cshtml’ to type ‘System.Web.WebPages.StartPage

Global.asax: (C# project)

    protected void Application_Init()
    {
        System.Web.WebPages.Razor.RazorBuildProvider.RegisterBuildProvider("cshtml", typeof(DefaultRazorBuildProvider));
    }

Class definitions: (VB Project)

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:25:24+00:00Added an answer on May 20, 2026 at 6:25 am

    Yes; in 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>
    

    EDIT

    To create the host in code, you can inherit RazorBuildProvider and override the CreateHost method, then register your inherited version for CSHTML and VBHTML.
    In your override, you’ll need to return a WebCodeRazorHost for pages inside of App_Code.

    You should download the source code; it will help tremendously.
    Look in mvc3-rtm-sources\webpages\src\System.Web.WebPages.Razor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in Android list view can we set some value to each row just like
Just like in HTML / CSS you can set the position of a div
NSString just like 1000.00 and 1008977.72, how can I format them to 1,000.00 and
How can I print any object into system clipboard just like printfn %A does?
How can I declare a method with keyword arguments just like rails do. some
Is there any way can declare a bean in just like JSP UseBean in
I need to create a wall system just like Facebook's (users can post messages,
Can I deploy SQL Server Express with my desktop application just like builtin database?
Can I use that effect on my controls just like the BlurEffect? If it's
How can you implement a RadioButtonPreference in android? Just like the CheckBoxPreference . Are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.