简体   繁体   English

ASP.NET MVC html助手无法正常工作

[英]ASP.NET MVC html helpers not working

I hope somebody can help me. 我希望有人可以帮助我。 I've been trying to write a custom html helper for my MVC application. 我一直在尝试为我的MVC应用程序编写一个自定义的html助手。 First at all, i tried with a testing one, which only writes a 首先,我尝试了一个测试,只写了一个

tag for the specified param. 指定参数的标记。 The things is, it does not work unless I explicitly import the namespace. 事情是,除非我显式导入命名空间,否则它不起作用。 I've been reading a lot and as i read, That method should appear without the import namespace like this: 我一直在阅读,当我阅读时,该方法应该没有像这样的导入命名空间:

<%=Html.Prueba("This is a paragraph") %>

But this method, Prueba, is not showing up in the VS Intellisense. 但是这种方法Prueba没有出现在VS Intellisense中。

My Class is the following: 我的班级如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace EasyGoCostaRica.Helpers
{
    public static class ViewsHelpers
    {
        //This method is just for testing. Is not working :(
        public static string Prueba(this HtmlHelper helper, string param1)
        {
            return string.Format("<p>{0}</p>", param1);
        }
    }

}

Thanks in advance! 提前致谢!

Namespace must be declared/imported somewhere. 必须在某处声明/导入命名空间。 You can do that either: 你也可以这样做:

  • within the page itself 在页面内部
  • master page or 母版页或
  • inside web.config file 在web.config文件中

If you want something global it's best to configure your namespace in web.config. 如果你想要全局的东西,最好在web.config中配置你的命名空间。

Use <@import...> directive for the first two and <namespace> configuration element for the last one. 前两个使用<@import...>指令,最后一个使用<namespace>配置元素。

You can add the namespace to the web.config and then you won't have to worry about it later. 您可以将命名空间添加到web.config中,然后您不必再担心它。

Inside your web.config, you should see something like this: 在你的web.config中,你应该看到这样的东西:

<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.Linq"/>
<add namespace="System.Collections.Generic"/>
</namespaces>

Just add a line with your namespace. 只需在命名空间中添加一行即可。

If you don't want the helpers to be globally imported, each directory can have it's own web.config. 如果您不希望全局导入帮助程序,则每个目录都可以拥有自己的web.config。 Unless specifically set, those "sub" web.configs will inherit settings from higher web.configs. 除非特别设置,否则那些“sub”web.configs将继承更高web.configs的设置。 If you go this route, be forewarned, some settings can only be set at the application level. 如果你走这条路线,要预先警告,一些设置只能在应用程序级别设置。 It can get confusing fast. 它可以快速混淆。

出于某种原因,在visual studio 2013中,您必须重新启动vs才能应用web.config中的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM