Defining an alias for a class with Razor

In regular C # code, I can use the using statement to define an alias for the class name, for example.

using MyAlias = Some.Long.Namespace.Class; 

I tried the same thing in a razor, a naive approach like

 @using MyAlias = Some.Long.Namespace.Class 

does not work. Is there a way to achieve the same effect?

+6
source share
1 answer

Why do you need this? Whatever the reason you need is probably the best way. In any case, you should avoid writing C # code in Razor mode, so you won’t need it. All you need in the Razor view is the namespace for your view model, because everything that needs to be processed must be processed.

 @model MyViewModel ... 

Leave the aliases and C # code where they belong - controllers, models, helpers, ...

All of this says that aliases should work. For example, the following view works fine for me:

 @using foo = System.IO; <div> @foo.Path.GetFileName(@"c:\work\foo.txt") </div> 
+11
source

Source: https://habr.com/ru/post/906556/


All Articles