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?
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.
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.
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.
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.
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