【问题标题】:Access methods or properties from html in asp.net mvc从asp.net mvc中的html访问方法或属性
【发布时间】:2010-12-23 04:53:22
【问题描述】:

好吧,这看起来很简单:

在 c# asp.net mvc 中,我可以声明一些公共类,例如:

public class Foo {
   public static string Bar {get
   {return "bar";}
   }
}

并从任何 html 访问它,例如:

<%=Foo.Bar;%>

对吗?

好吧,我必须在 ASP.NET MVC VB.Net 中做同样的事情,但我无法访问任何变量或方法:

public class Foo
   public Shared ReadOnly Bar as string
   Get
      return "Bar"
   end Get
   End Property
End Class

试图做 在 vb.net 中不起作用我得到 Name 'Foo' is not declared.

我在这里错过了什么?



更新:
好的标准 VB.NET 将模块放在项目中定义的命名空间中。

我必须输入

MVCApplication1.Foo.Bar

关于如何创建一个不将其包装到命名空间中的模块有什么建议吗?还是让视图默认导入应用命名空间?


更新:
哦,我自己也可以回答这个问题:在 web.config 中:

【问题讨论】:

  • 在我看来问题不在于 VB 与 C#。检查 1) 您的 web.config 是否引用了您的 VB.NET 程序集;和 2) 你正在使用正确的命名空间。

标签: asp.net asp.net-mvc vb.net


【解决方案1】:

您可以将导入的命名空间添加到 web.config:

<configuration>
<system.web>
    <pages>
        <namespaces>
            <add namespace="MyCompany.Application" />
        </namespaces>
    </pages>
</system.web></configuration>

【讨论】:

    【解决方案2】:

    重新编译你的项目

    哦,您的属性缺少属性关键字

    试试这个:

        Public Shared ReadOnly Property Bar As String
        Get
            Return "Bar"
        End Get
        End Property
    

    【讨论】:

    • 当然我已经编译了我的项目,你看到的代码是伪代码。我不会在现实世界中创建 foo bar 类。
    • 您是否对 web.config 中的程序集有参考?
    猜你喜欢
    • 2012-06-13
    • 2011-02-08
    • 2012-07-20
    • 1970-01-01
    • 2011-04-13
    • 2011-08-11
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    相关资源
    最近更新 更多