Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alias a type with a using directive in my Razor view?

I need to alias a type with a using directive, which is normally done like so:

using LocalResources = Resources.Admin.SomeResource;

How can I do this inside my Razor view so that I can then use it like @LocalResources.FirstName?

like image 247
ShaneKm Avatar asked Jan 25 '11 20:01

ShaneKm


People also ask

What can the @page directive do in a Razor page?

The Razor content page requires the @page directive at the top of the file. The HasFormContentType property is used to determine whether a form has been posted and the Request. Form collection is referenced within a Razor code block with the relevant value within it assigned to the name variable.

Which directive helps Cshtml?

cshtml file, you will still need to use @using . The @namespace directive was designed so the C# classes added to a project and pages-generated code just work without having to add an @using directive for the code behind file.

What is @model in Razor?

New @model directive Let's now look at a new feature we added with the ASP.NET MVC 3 Beta – the @model directive. The @model directive provides a cleaner and more concise way to reference strongly-typed models from view files.


2 Answers

Here is how

@using File = System.IO.File //at the top of the page then 
// use @File any where in the cshtml page.
like image 69
Surjit Samra Avatar answered Oct 12 '22 14:10

Surjit Samra


Razor does not currently support type or namespace aliasing. This is something that we might consider for future versions.

like image 32
marcind Avatar answered Oct 12 '22 13:10

marcind