开发者

Adding namespaces to ASP.NET MVC 3 views

I tried adding namespaces to configuration/system.web/pages/namespaces in the web.config of my ASP.NET MVC 3 application so I could us开发者_Python百科e classes in those namespaces in my views without needing a @using, however this has no effect. How can I add namespaces to my views?


MVC razor has a different area for namespaces.

Look in the second web.config, the one in your Views folder and add namespaces this way.

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="Telerik.Web.Mvc.UI;"/>
        </namespaces>
    </pages>
</system.web.webPages.razor>


I'm not sure why it's so flaky, but chances are that it is actually working, but Visual Studio doesn't recognize it until you close and re-open the view you're in. Also make sure you're in the web.config that's located in the Views directory.


I used this method to include Resource files into my views but Razor didn't want to pick up my classes. After some hair-pulling I realized I didn't change the Access Modifier on the Resources.resx file to public (it's created as Internal by default). Once I changed it & recompiled, I could access my resources from all views.

Rookie mistake, hate to see it happen, but hopefully it'll save someone some pain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜