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="TestCode.CustomWebRazorHostFactory" />
Bingo!
source
share