How do I create a WebRazorHostFactory that does no

2019-08-06 07:35发布

问题:

If I create a new WebRazorHostFactory like this:

namespace Test
{
    public class TestMvcWebRazorHostFactory : WebRazorHostFactory
    {
        public override WebPageRazorHost CreateHost(string virtualPath, string physicalPath)
        {
            WebPageRazorHost host = base.CreateHost(virtualPath, physicalPath);

            if (!host.IsSpecialPage)
            {
                return new MvcWebPageRazorHost(virtualPath, physicalPath);
            }

            return host;
        }
    }
}

and reference it in ~/Views/Web.config like this:

<system.web.webPages.razor>
    <host factoryType="Test.TestMvcWebRazorHostFactory" />
    ...
</system.web.webPages.razor>

the intellisense in my view files no longer recognize the "model" and "TextBoxFor" in:

@model Test.Models.Person

<div>
    @this.Html.TextBoxFor( x => x.Name )
</div>

How do I make a new WebRazorHostFactory that doesn't mess up the intellisense in the view files?

In the MvcWebPageRazorHost I see they run a function "GetRidOfNamespace("System.Web.WebPages.Html");", but I'm not sure if this is the problem? I have tried to write a different class that does the same thing as MvcWebPageRazorHost without this function and it seems to have no effect. Thanks.

回答1:

I got it working by changing it at runtime in my Application_Start in Global.asax.cs.

Here's the code:

        var hostSection = WebConfigurationManager.GetSection(HostSection.SectionName, "/Views") as HostSection;

        if (hostSection != null)
        {
            hostSection.FactoryType = "Type.Name, Assembly";
        }

This would need to be done for any areas you might have as well.



回答2:

You need to install the assembly that contains the custom factory into the GAC so Visual Studio can use it.