Removing System.Web.UI from a Razor View

The namespace is System.Web.UIimported by default in the Razor template.

How can i avoid this?

+1
source share
1 answer

I'm not sure if this is what you need, but I did a quick experiment. I think it works.

I have expanded MvcWebRazorHostFactory a bit. The new factory basically goes through the host created by the base class, but first removes the namespace.

This is the class:

namespace TestCode
{
    using System.Web.WebPages.Razor;
    using System.Web.Mvc;
    using System.Web.Mvc.Razor;

    public class CustomWebRazorHostFactory : MvcWebRazorHostFactory
    {
        public override WebPageRazorHost CreateHost(string virtualPath, string physicalPath)
        {
            WebPageRazorHost host = base.CreateHost(virtualPath, physicalPath);
            host.NamespaceImports.Remove("System.Web.UI");
            return host;
        }
    }
}

Then I replaced web.config with using my factory instead of the standard one:

<system.web.webPages.razor>
    <!--<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />-->
    <host factoryType="TestCode.CustomWebRazorHostFactory" />

Bingo!

+1
source

Source: https://habr.com/ru/post/1530683/


All Articles