Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the default Razor cshtml namespaces defined?

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.

like image 439
Jack Ukleja Avatar asked Oct 22 '22 17:10

Jack Ukleja


1 Answers

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.

like image 100
Jack Ukleja Avatar answered Oct 28 '22 21:10

Jack Ukleja