How to import a namespace in Razor View Page?
In my webpages I have references to js and images as such: "../../Content/Images/"Filename" In my code if I reference a file as above, it doesnt work so i have to write: "c:/miscfiles/"filename" 1-...
How to import a namespace in Razor View Page?
Finally found the answer.
@using MyNamespace
For VB.Net:
@Imports Mynamespace
Take a look at @ravy amiry s answer if you want to include a namespace across the app.
The first way is that use @using
statement in .cshtml
files, that imports a namespace to current file only, and the second:
In the "web.config" file in "Views
" directory of your project (notice it is not the main web.config in project s root), find this section:
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
.
.
<!-- etc -->
</namespaces>
</pages>
</system.web.webPages.razor>
you can add your custom namespace like this:
<add namespace="My.Custom" />
that will add the namespace to all of .cshtml (and/or .vbhtml) files; also you can change views inheritance from here, like:
<pages pageBaseType="My.Custom.MyWebViewPage">
Regards.
UPDATE: Thanks to @Nick Silberstein
to his reminder about areas! He said:
If you re working within an area, you must add the namespace
within the Web.config
under /Areas/<AreaName>/Views/
rather than
/Views/
For Library
@using MyNamespace
For Model
@model MyModel
In ASP.NET MVC 3 Preview1 you can import a namespace on all your razor views with this code in Global.asax.cs
Microsoft.WebPages.Compilation.CodeGeneratorSettings.AddGlobalImport("Namespace.Namespace");
I hope in RTM this gets done through Web.config section.
I found this http://weblogs.asp.net/mikaelsoderstrom/archive/2010/07/30/add-namespaces-with-razor.aspx which explains how to add a custom namespace to all your razor pages.
Basically you can make this
using Microsoft.WebPages.Compilation;
public class PreApplicationStart
{
public static void InitializeApplication()
{
CodeGeneratorSettings.AddGlobalImport("Custom.Namespace");
}
}
and put the following code in your AssemblyInfo.cs
[assembly: PreApplicationStartMethod(typeof(PreApplicationStart), "InitializeApplication")]
the method InitializeApplication will be executed before Application_Start in global.asax
One issue that you must know is that when you import a namespace via web.config
in Views
folder, that namespace
is imported JUST for views in that folder. Means if you want to import a namespace
in an area views, you must also import that namespace
, in that area s web.config
file, located in area s Views
folder;
For namespace and Library
@using NameSpace_Name
For Model
@model Application_Name.Models.Model_Name
For Iterate the list on Razor Page (You Have to use foreach loop for access the list items)
@model List<Application_Name.Models.Model_Name>
@foreach (var item in Model)
{
<tr>
<td>@item.srno</td>
<td>@item.name</td>
</tr>
}
You can try this
@using MyNamespace
"using MyNamespace" works in MVC3 RTM. Hope this helps.
I think in order import namespace in razor view, you just need to add below way:
@using XX.YY.ZZ
Depending on your need you can use one of following method:
if required in all subsequent views then add "using your.domainName;" in _ViewStart.cshtml. You can find more about this in: Where and how is the _ViewStart.cshtml layout file linked?
Or add Assembly reference in View web.config as described by others explained in: How do you implement a @using across all Views in Asp.Net MVC 3?
In my webpages I have references to js and images as such: "../../Content/Images/"Filename" In my code if I reference a file as above, it doesnt work so i have to write: "c:/miscfiles/"filename" 1-...
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
I m looking for best practices here. Sorry. I know it s subjective, but there are a lot of smart people here, so there ought to be some "very good" ways of doing this. I have a custom object called ...
I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...
i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...
For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?
I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!