ASP.NET Views: Avoiding using <%@ Assembly... in every .aspx
Virtually every .aspx page I have in my web site needs to have this at its top to functi开发者_如何学编程on correctly:
<%@ Assembly Name="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
Is there anyway I can avoid having to declare this in the .aspx view for every page? Isn't there some way I can declare this globally for all .aspx views? Maybe something in the web.config?
Add it to assemblies
<assemblies>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
The @Assembly directive correspond to assemblies tag in web.config not namespace tag. Check MSDN reference
You can declare it in web.config in the assemblies
section, like this:
<system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=3.0.0.0 ... "/>
</assemblies>
</compilation>
</system.web>
However, according to the MSDN docs:
Assemblies that reside in your Web application's \Bin directory are automatically linked to ASP.NET files within that application. Such assemblies do not require the @ Assembly directive. You can disable this functionality by removing the following line from the section of your application's Web.config file:
<add assembly="*"/>
As others have pointed out, you can declare this in the web.config pages section.
Another alternative (if its available to you) is to use the new Razor View engine. It not only removes this type of code, but also provides cleaner, overall syntax. Of course, I realize this may not be a viable solution as you may be limited by your current technology/customer needs/etc.
An example of what you may see at the top of a Razor page is shown here:
@model Some.StronglyTyped.Model
@using Other.Libraries.To.Import
@{
ViewBag.Title = "Specific Page Title";
}
Put it in the Web.config as a global namespace. It will be available to all your pages there.
<system.web>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web>
精彩评论