在Web.Config中添加的HtmlHelper命名空间不工作 [英] Adding HtmlHelper NameSpace in Web.Config does not work

查看:239
本文介绍了在Web.Config中添加的HtmlHelper命名空间不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题否1:

我已经开始学习ASP.NET MVC大家好,我做了一个简单的扩展方法,像这样

 命名空间MvcTestz //项目也被命名为MvcTestz
{
  公共静态类SubmitButtonHelper //扩展方法。
  {
    公共静态字符串提交按钮(这的HtmlHelper帮手,串buttonText)
    {
        返回的String.Format(<输入类型= \\提交\\价值= \\{0} \\>中,buttonText);
    }
  }
}

web.config中的自定义的HtmlHelper。然后我加入命名空间,这样

 <&命名空间GT;
    <! - 其他命名空间 - >
    <添加命名空间=MvcTestz/>
    <! - 其他命名空间 - >
  < /命名空间>

这样我就可以在剃须刀视图中使用智能感知,但它的自定义帮助剪掉在一个视图(家庭/查看/ About.cshtml)出现了


因此​​,在另一种观点认为(家庭/查看/ Index.cshtml)我用 @using MvcTestz添加命名空间; 声明。

问题否2

Web应用程序后执行首页(家庭/查看/ Index.cshtml)不显示渲染成HTML输入按钮上的文字。


在关于页(家庭/查看/ About.cshtml)断绝生成错误。 (点击放大)

更新:


  1. 智能感知问题解决了,我在查看Directory.SOLVED编辑Web.config present。

  2. HtmlString 应,如果我想呈现一个HTML button.SOLVED使用。


解决方案

问题1:

剃刀命名空间应该在registred的< system.web.webPages.razor> 在web.config中的节点:

 < system.web.webPages.razor>
    <主机factoryType =System.Web.Mvc.MvcWebRazorHostFactory,System.Web.Mvc,版本= 3.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
    <页面pageBaseType =System.Web.Mvc.WebViewPage>
      <&命名空间GT;
        <添加命名空间=System.Web.Mvc/>
        <添加命名空间=System.Web.Mvc.Ajax/>
        <添加命名空间=System.Web.Mvc.Html/>
        <添加命名空间=System.Web.Routing/>
        <添加命名空间=MvcTestz/>      < /命名空间>
    < /页>
  < /system.web.webPages.razor>

问题2:使用 HtmlString ,而不是放在你的助手字符串:

 公共静态HtmlString提交按钮(这的HtmlHelper帮手,串buttonText)
{
    返回新HtmlString(的String.Format(<输入类型= \\提交\\价值= \\{0} \\>中,buttonText));
}

Problem No 1:

Hi guys I have started learning ASP.NET MVC , I have made a simple extension method ,like this

namespace MvcTestz  //Project is also named as "MvcTestz"
{
  public static class SubmitButtonHelper //extension method.
  {
    public static string SubmitButton(this HtmlHelper helper,string buttonText)
    {
        return string.Format("<input type=\"submit\" value=\"{0}\">",buttonText);
    }
  }
}

Then I have added namespace of the Custom HtmlHelper in to the Web.Config ,like this

  <namespaces>
    <!--other namespaces-->
    <add namespace="MvcTestz"/>
    <!--other namespaces-->
  </namespaces>

So that I could use intellisense in the razor View ,but it custom Helper didnt showed up in one View (Home/View/About.cshtml).

So in another view (Home/View/Index.cshtml) I added namespace by @using MvcTestz; statement.

Problem No 2:

Upon WebApp execution Home page(Home/View/Index.cshtml) shows input button text without rendering it into HTML.

On the About page (Home/View/About.cshtml) sever generates error. (Click for Enlarged)

Update:

  1. Intellisense problem solved , I had to edit the Web.Config present in the View Directory.SOLVED.
  2. HtmlString Should be used if I want to render a Html button.SOLVED.

解决方案

Problem 1:

Razor namespaces should be registred in the <system.web.webPages.razor> node in web.config:

 <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.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="MvcTestz"/>

      </namespaces>
    </pages>
  </system.web.webPages.razor>

Problem 2: Use HtmlString instead of string in your helper:

public static HtmlString SubmitButton(this HtmlHelper helper, string buttonText)
{
    return new HtmlString(string.Format("<input type=\"submit\" value=\"{0}\">", buttonText));
}

这篇关于在Web.Config中添加的HtmlHelper命名空间不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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