Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP MVC Razor view extension methods, how to create 'global' view methods?

Tags:

asp.net-mvc

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

like image 345
house9 Avatar asked Oct 20 '10 16:10

house9


People also ask

What is difference between View and razor view?

The Razor View Engine is a bit slower than the ASPX View Engine. Razor provides a new view engine with streamlined code for focused templating. Razor's syntax is very compact and improves readability of the markup and code. By default MVC supports ASPX (web forms) and Razor View Engine.

What is MVC Razor view?

Razor is a templating engine and ASP.NET MVC has implemented a view engine which allows us to use Razor inside of an MVC application to produce HTML. However, Razor does not have any ties with ASP.NET MVC. Now, Razor Syntax is compact which minimizes the characters to be used, however it is also easy to learn.


2 Answers

As remarked by others, Razor Views all ultimately inherit from WebViewPage:

public abstract class WebViewPage<TModel> : WebViewPage

You can therefore simply write your extension methods for WebViewPage without creating a new base class or changing the config files, which has been suggested by other answers. For example:

public static class WebViewPageExtensions
{
    public static string HollowWorld(this WebViewPage wvp)
    {
        return "Memento mori";
    }
}

Add a using statement for that namespace to your View and then:

<p>@this.HollowWorld()</p>
like image 122
Rob Kent Avatar answered Oct 16 '22 01:10

Rob Kent


it turns out, the asp runtime is going to define the Execute method at runtime, so the custom view base class must also be abstract

using System;
using System.Web.Mvc;

namespace MyMvcWebApp.Extensions
{
    public abstract class ViewBase<TModel>
        : System.Web.Mvc.WebViewPage<TModel> where TModel : class
    {
        // now this will be available in any view @HelloWorld()
        public string HelloWorld()
        {
            return "Hello from the ViewBase class";
        }
    }
}

this should work with strongly typed views, it looks like with razor all views are strongly typed, when you do not define the type 'dynamic' is used and that is the strong type

also, as Clicktricity stated you then update the web.config (the one under the Views directory)

<pages pageBaseType="MyMvcWebApp.Extensions.ViewBase">
like image 36
house9 Avatar answered Oct 16 '22 01:10

house9