Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public variables in MVC 3 Razor _ViewStart

I'm building a site on the new Razor engine that comes with MVC 3 (and loving the new syntax!). However, I am at a loss about using public properties / constants with it. I know that with WebForms we could add a public property in 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 paths to CSS files and images:

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

I have tried putting the above code block in _Layout, as well as inside _ViewStart. However, accessing them from child views fails miserably. I thought of defining a public property in the above code block but it doesn't compile.

Solutions?

  • As far as I have seen, noone uses code behind with Razor.
  • I guess I should be able to inherit from the default view and define my properties there (as described on Stack).

But I'm strongly hoping that there should be an easier way to do something so simple?

like image 398
Dav Avatar asked Nov 27 '10 11:11

Dav


People also ask

What is the use of _ViewStart Cshtml in MVC?

The _ViewStart. cshtml page is a special view page containing the statement declaration to include the Layout page. Instead of declaring the Layout page in every view page, we can use the _ViewStart page. When a View Page Start is running, the “_ViewStart.

How do you declare a global variable in razor?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

What is _layout Cshtml in MVC?

The file MasterLayout. cshtml represents the layout of each page in the application. Right-click on the Shared folder in the Solution Explorer, then go to Add item and click View. Copy the following layout code.

How do I add a layout in razor view?

To create a new layout view in Visual Studio, right-click on the Shared folder -> select Add -> click on New Item... This will open the Add New Item popup, as shown below. In the Add New Item dialogue box, select MVC 5 Layout Page (Razor) template, and specify a layout view name as _myLayoutPage.


2 Answers

I decided to follow yet another path, and extended UrlHelper to provide paths to all three folders 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 good to go... almost :-) I'm now wondering if there's a place where I would be able to define the using MyNamespace.Helper statement would go in order for these extension methods to be available application-wide. In the old days we would add an entry in 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 a using statement in _ViewStart.cshtml but no luck either - the only way for my extension methods to be visible is to add a using statement on a particular page, which again isn't ideal.

Any suggestions? Have any of you seen an explanation of Razor's order of page parsing & delivery?

like image 176
Dav Avatar answered Sep 25 '22 15:09

Dav


Your can create a folder "App_Code" and create a file "GlobalVal.cshtml". bellow is a sample code in the file:

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

and bellow is a sample using it:

<a href="@GlobalVal.__siteHome/home/index">@GlobalVal.__siteHome</a>
like image 42
Allen Fu Avatar answered Sep 22 '22 15:09

Allen Fu