By default the following appear in code generated by Razor:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Helpers;
using System.Web.Security;
using System.Web.UI;
using System.Web.WebPages;
Where do these come from? I have looked in the machine.config and root-level web.config and it's not in there.
I have verified I can add to that list by using:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
.... etc
And I'm guessing I might be able to remove using <clear/>
...but I'm curious to know where they come from to begin with.
It appears these default namespace are hard coded in WebPageRazorHost.cs and MvcWebPageRazorHost.cs (links to Github).
The former adds all the namespaces in the constructor (including System.Web.WebPages.Html
), and the latter removes it, using a method called GetRidOfNamespace
(!)
I tried calling <clear/>
but sadly/surprisingly it had no effect.
NamespaceImports
is a public member of RazorEngineHost
so hopefully one can tweak it in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With