开发者

Calling extension helpers from inline helpers - how?

开发者 https://www.devze.com 2023-03-11 00:22 出处:网络
I read this [useful article] that says I can create a library of inline helpers by putting them in a view in the special folder App_Code.When I moved my @helper functions there, calls to extension hel

I read this [useful article] that says I can create a library of inline helpers by putting them in a view in the special folder App_Code. When I moved my @helper functions there, calls to extension helpers I have开发者_开发问答 stopped working. I read [in this SO article] that there's an issue because the @helpers are static but my extensions are not... I tried the 2 different ways but cannot make it work. It fails to recognise the existence of my extension helpers.

'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Image'

my extension helper is called 'Image'. What should I be looking for?


When you write the Helperextension in razor view. You need to call it like FileName.Method.

eg you have CustomHelpers.cshtml file in app_code and in that you have a method

     @helper TruncateString(string input, int length)
     {        
        if (input.Length <= length)
        {      
            @input        
        }
        else
        { 
          @input.Substring(0, length)<text>...</text>        
        }
    }

you can call it from index.cshtml

    @CustomHelpers.TruncateString("Example", 8); 


As a matter of example, the following code use the "RenderPage" function within a helper library:

@helper GetSection(string sectionName, string sectionTitle, string sectionFileName){
    <div id="@sectionName">
        <h1>@sectionTitle</h1>
        @System.Web.WebPages.WebPageContext.Current.Page.RenderPage("~/Views/Shared/MySections/" + @sectionFileName)
    </div>
}

It demonstrate how to retrieve the "current page" (context sensitive) instance.

This code works fine from within your own "common library" Razor helper file (".cshtml") situated in the "*app_code*" subfolder of your project.


ok, my problem had to do with namespace... so I have in \Views\Shared\HtmlHelpers.cs:

public static class Html
{
    public static MvcHtmlString Image(this HtmlHelper helper, string src, object htmlAttrs = null)
    {

which I generally access from my pages like this:

@Html.Image("/path/to/image")

in App_Code\Helpers.cshtml:

@helper AddButton(string path)
{
    var Html = ((System.Web.Mvc.WebViewPage) WebPageContext.Current.Page).Html;
    @Html.Image(path);
}

but Intellisense would underline the "Image" and complain:

'System.Web.Mvc.HtmlHelper<object>' does not contain a definition for 'Image'
and no extension method 'Image' accepting a first argument of type 
'System.Web.Mvc.HtmlHelper<object>' could be found (are
you missing a directive or an assembly reference?)

the reason seemed to be that the Helpers.cshtml needs to have @using for the namespace... on my regular pages the namespace is included in my web.config but this page seems exempt from that mechanism

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号