Razor: Declarative HTML helpers

Deadly 提交于 2019-11-26 05:29:22

问题


I\'m trying to write a simple declarative html helper:

@helper Echo(string input) {
    @input
}

The helper works fine if I embed it into the page I want to use it on. But if I move it to a separate .cshtml file and place that file in the ~/Views/Helpers directory, my view can\'t be compiled anymore because the helper is not found. According to Scott Gu\'s blog article on Razor it should work.

What am I doing wrong?


回答1:


The ~/Views/Helpers location as describe in that ScottGu post will not work in MVC 3 RTM. At the time that article was written it was a design idea we were tossing around but ultimately we were not able to implement it.

Putting your helpers in App_Code works but has certain limitations that impact certain MVC scenarios (for example: no access to standard MVC Html. helpers)




回答2:


Simple workaround:

In each helper add argument WebViewPage page.

App_Code\Test.cshtml:

@using System.Web.Mvc.Html
@using System.Web.Mvc

@helper HelloWorld(WebViewPage page)
{
    @page.Html.Label("HelloWorld")
}

This helper usage in any page:

 @Test.HelloWorld(this)



回答3:


As marcind said, we weren't able to support the ~/Views/Helpers location due to some limitations in our compilation model.

We're working on a better solution for declarative HTML helpers, but it won't make v1. For v1, we have two solutions:

  1. App_Code (which has some problems as Marcin mentioned, but does work)
  2. David Ebbo (member of the team) has a Visual Studio add-in that compiles them into your DLL



回答4:


In App_Code a @helper needs this to work properly:

// These are important for `Html.RouteLink` and such.
@using System.Web.Mvc;
@using System.Web.Mvc.Routing;
@using System.Web.Mvc.Html;
@using System.Web.Mvc.Razor;

@helper SomeHelper()
{
    // Get page and pull helper references from it.
    var wvp = PageContext.Page as System.Web.Mvc.WebViewPage;
    var Url = wvp.Url; // UrlHelper access
    var Html = wvp.Html; // HtmlHelper access
    var ViewBag = wvp.ViewBag;
    // Helper code comes here...
}



回答5:


I followed the steps listed in Scott's blog as well and wasn't able to get it working either.

I did some searching and found this link: http://dotnetslackers.com/articles/aspnet/Experience-ASP-NET-MVC-3-Beta-the-Razor-View-Engine.aspx#s19-create-custom-helper-method

Followed the steps and it's working. The key seems to be both the App_Code folder as well as using the file name dot helper name when calling the helper.




回答6:


I know this is late in the game... But, pass in the current View to the helper function. From the view you have access to Html and the helper functions.




回答7:


Another take on CodeAngry's answer, this enables the helpers for every method in the file.

@using System.Web.Mvc;
@using System.Web.Mvc.Html;
@using System.Web.Mvc.Routing;
@using System.Web.Mvc.Razor;

@functions {
    private static WebViewPage page { get { return PageContext.Page as WebViewPage; } }
    private static System.Web.Mvc.HtmlHelper Html { get { return page.Html; } }
    private static UrlHelper Url { get { return page.Url; } }
    private static dynamic ViewBag { get { return page.ViewBag; } }
}


来源:https://stackoverflow.com/questions/4451287/razor-declarative-html-helpers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!