RazorGenerator无法看到自定义CSHTML帮手 [英] RazorGenerator can't see custom cshtml helper

查看:562
本文介绍了RazorGenerator无法看到自定义CSHTML帮手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与RazorGenerator一个问题:它不能编译观点,即使用我的自定义帮助:

I'm having a problem with RazorGenerator: it can't compile views that uses my custom helper:

App_Code文件/ ViewHelper.cshtml

App_Code/ViewHelper.cshtml

@helper test(System.Web.Mvc.HtmlHelper html)
{
    <h4>Test</h4>    
}



查看/ Test.cshtml

Views/Test.cshtml

(...)
@ViewHelper.test(this.Html)
(...)

当我尝试编译我的项目,我得到这个错误Test.cshtml:

When I try to compile my project I get this error for Test.cshtml:

ViewHelpers并不在当前的背景下

The name 'ViewHelpers' does not exist in the current context

我尝试添加各种命名空间我的意见/web.config文件:

I tried adding various namespaces to my Views/web.config file:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Optimization" />
        <add namespace="MyProject" />
        <add namespace="Kendo.Mvc.UI" />
        <add namespace="MvcSiteMapProvider.Web.Html" />
        <add namespace="MvcSiteMapProvider.Web.Html.Models" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>



但没有成功...

but with no success...

我缺少一些空间应包含在这个文件?如果没有,是否有任何其他方式使RazorGenerator编译看法? ?此外,没有CSHTML助手甚至在一个命名空间中

Am I missing some namespace that should be included in this file? If not, is there any other way to make RazorGenerator compile the view? Also, does cshtml helpers even exist in a namespace?

推荐答案

您需要把这个评论对你有帮助的开始 App_Code文件

Solution:

You need to put this comment at the beginning of a helper you have in App_Code:

@*
    Generator: MvcHelper
    GeneratePrettyNames : true
*@

然后在的web.config ,你必须为你需要添加RazorGenerator对于那些佣工产生的命名空间剃须刀页面配置:

Then in web.config where you have configuration for razor pages you need to add namespace that RazorGenerator generated for those helpers:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>                
            <add namespace="YourWebAppNamespace.App_Code" />
        </namespaces>
    </pages>
</system.web.webPages.razor>



修改 YourWebAppNamespace 为您默认的项目命名空间(它可能是你的项目名称):

Change YourWebAppNamespace to you default project namespace (it's probably your project name):

< IMG SRC =http://i.stack.imgur.com/eZZGj.pngALT =默认的命名空间在Visual Studio项目>

RazorGenerator对待你像正常的Razor视图助手这样生成的代码是这样的:

RazorGenerator treated you helpers like normal razor view so the generated code looked like this:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "2.0.0.0")]
    [System.Web.WebPages.PageVirtualPathAttribute("~/App_Code/TestHelper.cshtml")]
    public partial class _App_Code_TestHelper_cshtml : Komplett.Chrome.Web.Features.Shared.BaseView<dynamic>
    {

#line 3 "..\..\App_Code\TestHelper.cshtml"
public System.Web.WebPages.HelperResult HelperName(string name) {

您需要添加这些指令的意见告诉剃须刀发生器用正常的名称创建类(同助手的文件名,你这样做与 GeneratePrettyNames 指令)和静态公共功能,因此它可以在其他视图中使用(由完成发电机: MvcHelper 指令)。
有了这些指导意见RazorGenerator产生这样的C#文件:

You need to add these directive comments to tell razor generator to create class with "normal" name (the same as helper file name, you do this with GeneratePrettyNames directive) and with static public function so it could be used in other views (done by Generator: MvcHelper directive). With those directive comments RazorGenerator generate C# file like this:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "2.0.0.0")]
    public class TestHelper : System.Web.WebPages.HelperPage
    {

#line 6 "..\..\App_Code\TestHelper.cshtml"
public static System.Web.WebPages.HelperResult HelperName(string name) {

现在只需要命名空间添加到的web.config 这样生成的C#对其他意见的代码将使用语句的该命名空间产生的帮手。

You now just need to add namespace to web.config so C# generated code for other views would have using statement with namespace of this generated helper.

这篇关于RazorGenerator无法看到自定义CSHTML帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆