tags:

views:

29

answers:

1

I am using Razor view with asp mvc preview 3

I am trying to create some methods which I would like available directly in the views. These are not really Html helper methods so I don't think extending HtmlHelper makes sense?

my goal, be able to call methods in the view i.e.

@HelloWorld(); vs @Html.HelloWorld()

I can get Html.HelloWorld to work by creating an extension method on HtmlHelper

public static class HtmlExtensions
{
    public static string HelloWorld(this HtmlHelper helper)
    {
        return "Hello";
    }
}

I would like to do the same thing but for the view; my problem - what type of object is the view?

Note: I was able to get this to work by defining the methods in the .cshtml page

@functions 
{
    public string HelloWorld()
    {
        return "Hello";
    }
}

@HelloWorld() @* now this works *@

then I tried to put this code my _viewstart.cshtml file thinking it would be available in all views but it was not

if I knew which type the view was I think it could be easily extended, any help appreciated

A: 

The default base class for Razor views is specified in the Web.config located in the views directory. Usually it is:

<pages pageBaseType="System.Web.Mvc.WebViewPage">

I've not tried it, but I would suggest inheriting from this base class and adding your own functionality, then adjust the web.config accordingly.

Clicktricity
I tried this but get a compile error -> does not implement inherited abstract member 'System.Web.WebPages.WebPageExecutingBase.Execute()'; I need to do some more research; guessing that method normally gets generated at runtime by asp.mvc?
house9