39

I have functions in my view that is shared by several pages:

@functions 
{
    public HtmlString ModeImage(ModeEnum mode) 
    {
        switch(mode)
        {
            case AMode: new HtmlString("<img etc..."); break;
            // more etc...
        }
    }
}

Is there a way to have it in a separate file and include it on each page without having to copy and paste it in to each one. I know I can write a .cs file and access it per page, but the function really concerns the view and I'de hate to have to recompile if this function changes.

tereško
  • 58,060
  • 25
  • 98
  • 150
kailoon
  • 2,131
  • 1
  • 18
  • 33
  • 2
    What's wrong with recompiling? – SLaks Jun 14 '11 at 17:27
  • what about putting this function inside Layout view – Amir Ismail Jun 14 '11 at 17:34
  • 2
    @Miro: That won't work. Pages don't inherit anything from their layout pages. After all, `Layout` is set at runtime. – SLaks Jun 14 '11 at 17:49
  • See http://stackoverflow.com/questions/17602507/using-a-razor-functions-in-several-webpages-cshtml-files if you really need to reuse the same function over several distinct views. – rsenna Jan 24 '14 at 14:08

3 Answers3

46

(Here's a more detailed version of the existing answers.)

Create a folder called App_Code in the root of the MVC project if it doesn't already exist. In here, create an empty razor view and name it whatever you want:

MVC project with Razor view called Shared.cshtml inside App_Code folder

Add @helpers and/or static methods to it as needed:

@helper ShowSomething()
{
    <span>Something</span>
}

@functions
{
    public static int CalculateSomething()
    {
        return 1;
    }
}

Then use them from your views by first accessing the shared view by name:

@Shared.ShowSomething()
@Shared.CalculateSomething()
Sam
  • 40,644
  • 36
  • 176
  • 219
20

This sounds like you want the Razor @helper methods described in the blog post ASP.NET MVC3 and the @helper syntax within Razor by Scott Guthrie.

Here is the overview... "The @helper syntax within Razor enables you to easily create re-usable helper methods that can encapsulate output functionality within your view templates. They enable better code reuse, and can also facilitate more readable code."

vcsjones
  • 138,677
  • 31
  • 291
  • 286
Paige Cook
  • 22,415
  • 3
  • 57
  • 68
  • It doesn't look like he wants a helper (he's returning void) – SLaks Jun 14 '11 at 17:31
  • Sorry, it is sort of a helper method which returns an image html depending on an enum. I feel like its too little for a partial and I don't want code to be recompiled if I say, change the icon name. This looks like what I'm looking for. Going to try it out first. – kailoon Jun 14 '11 at 18:16
  • That was perfect. =) I didn't know this existed. Thank you very much. – kailoon Jun 14 '11 at 18:23
  • This is useful, but helpers have some limitations such as lack of generics, and you can still reuse static methods across multiple views. – Sam Nov 12 '14 at 02:25
4

You could make a static helper page and put a normal static method in the page.
You could then call it by writing PageName.MyMethod() anywhere, and I believe that you won't need to recompile the project.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964