Public Variables in MVC 3 Razor _ViewStart

I am building a site on the new Razor engine that ships with MVC 3 (and a loving new syntax!). However, I find it difficult to use public properties / constants with it. I know that with WebForms we could add a public property to the code behind:

public string ImageFolder { get; set; } 

I would like to define important variables in one global place that my views can access, starting with the paths to the CSS files and images:

 @{ Layout = "~/Views/Shared/_Layout.cshtml"; var ContentFolder = "~/Content"; var CssFolder = ContentFolder + "/Stylesheets"; var ImageFolder = ContentFolder + "/Images"; } 

I tried to put the above code in _Layout , as well as inside _ViewStart . However, access to them from ideas about children was unsuccessful. I was thinking about defining a public property in the above block of code, but it does not compile.

Solutions?

  • As far as I saw, nobody uses code with Razor.
  • I think I should inherit from the default view and define my properties there (as described in Stack ).

But I really hope that there should be an easier way to make something so simple?

+6
global-variables asp.net-mvc-3 razor
source share
6 answers

You can create the App_Code folder and create the GlobalVal.cshtml file. The following is sample code in a file:

 @functions{ public static readonly string __siteHome = "http://www.example.com"; public static readonly string __siteResource = "http://resource.example.com"; } 

and below - a sample with its use:

 <a href="@GlobalVal.__siteHome/home/index">@GlobalVal.__siteHome</a> 
+5
source share

I decided to follow another path and expanded UrlHelper to provide paths to all three folders that I think I might need:

 public static class ExtensionMethods { private const string ImagesFolder = "~/Images"; private const string StylesheetsFolder = "~/Stylesheets"; private const string ScriptsFolder = "~/Scripts"; public static string Images(this UrlHelper url) { return url.Content(ImagesFolder); } public static string Stylesheets(this UrlHelper url) { return url.Content(StylesheetsFolder); } public static string Scripts(this UrlHelper url) { return url.Content(ScriptsFolder); } } 

All that is needed ... almost :-) Now I wonder if there will be a place where I can define the using MyNamespace.Helper so that these extension methods are available for the whole application. In the old days, we would add an entry to web.config:

 <system.web> <pages> <namespaces> <add namespace="MyNamespace.Helper"/> </namespaces> </pages> </system.web> 

This doesn't seem to work with Razor :-( I tried adding the using statement to _ViewStart.cshtml , but no luck - the only way my extension methods were visible is to add the using statement on a specific page, which again is not ideal .

Any suggestions? Have any of you seen an explanation of the Razor order of parsing and page delivery?

+4
source share

Use the PageData property:

 @{ Layout = "~/Views/Shared/_Layout.cshtml"; PageData.Add("ContentFolder", "~/Content"); } 

and inside _Layout.cshtml:

 <%=PageData["ContentFolder"]%> 
+1
source share

In the _layout view

 @{ App.AnyName = "abc"; } 

Inherit mode

 @{ var anyVariable = App.AnyName; } 
+1
source share

Just put the constants in an open module inside your app_code folder or if you don't want to do this, just create classes in app_code and use the using (import) keyword to import the namespace (class name) in each and you can use it that way.

Alternatively, if it makes sense to do this, just add them to your view model - remember that it may not make sense to add these vars to your model, but it makes sense to add them to your view model! This is a view model, and this view model can capture constant values โ€‹โ€‹from a public module or class, or you can even set it in your real view model, so you will only define values โ€‹โ€‹in one place, and you do not need to use name import in each submission :)

Let me know how this happens, and if there is anything else I can do to help you.

In vb.net, but the same as csharp, and it is easy to understand, since it is vb.

 Public class YourModel // this is where you have the normal model you have... No big deal End Class ... // now you make the view model urself ... Public class MyViewModel Public MyNormalModel as YourModel //notice we r declaring ur normal model as a variable, u can use a property instead Public MyPathConstant1 as string = "abc" Public MyPathConstant2 as string = "abc" Public MyPathConstant3 as string = "abc" End Class 

Now you should set the value MyNormalModel for the instance of the current ur model, although you can do it in the ur controller, it is best to create a method inside the MyViewModel class that takes a copy of the current ur model as an argument and sets MyNormalModel to the current model, which we just passed in the argument.

You can still make this call in your controller, but on a different note, what people prefer to do, instead of passing the whole normal model as a property, just take the pieces and pieces that they need from the normal model and put them in the view (i.e. you may need only half the properties in the normal model for the view model). This is because, remember, the view model will be passed to the view, and they donโ€™t want to convey what they donโ€™t use :). But this means that you will need to set each of these properties one at a time most likely (unless those exact ones are encapsulated in a subclass that usually doesn't happen by chance lol).

I saved it in one so you can just copy the regular model in one shot for simplicity.

Now, when you pass the view model to your view (MyViewModel), you can use and access the normal model through the notation of the object and its properties, for example ... Model.MyNormalModel.Property1. Etc and do whatever you want with it in the view ... Alternatively, you can access the rest of your view model (the const values โ€‹โ€‹we set) like this ... Model.MyPathConstant1 and Model.MyPathConstant2 and etc. So you have access to almost everything you want, the normal ur model and everything else that you added later through what is now called your view model.

Please excuse sealed letters and ipad lol. Let me know if that makes sense.

0
source share

You can use the built-in property UrlHelper Content:

 @Url.Content("~/Content/Stylsheets") @Url.Content("~/Content/Images") 
0
source share

All Articles