Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally add "<@Assembly>" and "<@Import Namespace>" to Asp.net MVC views

Instead of writing

<@ Import Namespace="Microsoft.SharePoint" %>

on every view I create I know I can easily just edit my web.config file and add this:

...
<pages>
    <namespaces>
        <add namespace="Microsoft.SharePoint" />
    </namespaces>
</pages>

But this doesn't seem to work in design time. Visual Studio 2010 is not able to see SPContext unless I add these two lines on top of my view as well:

<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>

So how do I add assemblies globally as well as import namespaces so VS will be able to resolve classes/objects?

like image 787
Robert Koritnik Avatar asked Oct 06 '10 10:10

Robert Koritnik


People also ask

How do you add a namespace in razor view?

To import a namespace in Razor view, we use using statement as we write in C# class however it should be prefixed with @. Here we are importing namespace MVCTraining5. Utility namespace.

How do I add a namespace to ASPX page?

To add the namespace globally instead of page-by-page, just put the namespace in your web. config. You might have to restart Visual Studio for the IntelliSense to kick in. You can also create a mini web.

Why we use @using in MVC?

Every time you need to import a namespace inside your view. This is the same as when you import a namespace in a standard C# code file. So, if you need to use a type which is declared in a different namespace than the View one, you'll need to add a using statement before using that type.


1 Answers

You also need to add the assembly to the <assemblies> section of the <compilation> section under <system.web>:

<compilation debug="false" targetFramework="4.0">
  <assemblies>
    <add 
      assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  </assemblies>
</compilation>

Your targetFramework and debug attribute values may vary according to the framework version you're targetting and whether you're debugging or not.

like image 142
Kev Avatar answered Sep 23 '22 05:09

Kev