简体   繁体   English

为什么我可以在App_Code中访问ASP.NET项目而不是MVC2项目的静态类方法?

[英]Why can I access static class method in App_Code for an ASP.NET project NOT for an MVC2 Project?

In the same asp.net MVC2 project (no problem when it is a pure ASP.NET project), I have created 2 classes in App_Code without no namespace, one is normal class, the other is static. 在同一个asp.net MVC2项目中 (当它是纯ASP.NET项目时没有问题),我在App_Code中创建了两个没有命名空间的类,一个是普通类,另一个是静态的。

I can access public methods of instance class from anywhere, I cannot access public methods of the static class. 我可以从任何地方访问实例类的公共方法,而不能访问静态类的公共方法。 Why ? 为什么呢 And how can I access them then ? 那我该如何访问它们呢?

    public class MyWebservice
    {

        public stMyWebservice()
        {                                       

        }

        public String getText() {
        }
    }


    public static class Helper
    {

        public static String getText()
        {




        }
    }

I access the static class inside the index.aspx like this: 我这样访问index.aspx内部的静态类:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Helper.getText();                  
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewData["Message"] %></h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>
</asp:Content>

Be sure that the Build Action for the class is set to Compile . 确保该类的Build Action设置为Compile

Go to your App_Code folder, and select your class. 转到您的App_Code文件夹,然后选择您的课程。 Look in the properties page. 在属性页面中查找。 Find Build Action . 找到Build Action Set it to Compile . 将其设置为Compile Then you should have access to the methods and properties of that object elsewhere in your application. 然后,您应该可以在应用程序中的其他位置访问该对象的方法和属性。

Your static class should be accessible as the other class. 您的静态类应该像其他类一样可访问。 My guess you declared your static class without public access modifier, eg: 我猜你是在没有public访问修饰符的情况下声明了静态类,例如:

static class MyClass
{
}

If you declared the class in such a way, it will have internal access modifier. 如果以这种方式声明该类,则它将具有internal访问修饰符。 This means, it will be accessible within assembly. 这意味着,它将可以在组装中访问。 App_Code folder compiles into a single assembly and if you try to use you class outside the App_Code folder, you will get an error. App_Code文件夹编译为单个程序集,如果尝试在App_Code文件夹之外使用类,则会收到错误消息。

EDIT: one more - there is no difference whether the class declared as static or not in this case. 编辑:更多-在这种情况下,是否将类声明为static没有区别。 If the classes have the same access modifiers each class will be accessible in a place where the second is. 如果这些类具有相同的访问修饰符,则可以在第二个类所在的位置访问每个类。

So, as you provided not exact classes declaration, my second guess is: you have declared your static class inside a non-static one eg: 因此,由于没有提供确切的类声明,所以我的第二个猜测是:您已在非静态类中声明了静态类,例如:

public class MyWebservice
{
    public static class Helper
    {
        public static String getText()
        {
        }
    }
}

In this case the static class is also accessible through the parent class name: 在这种情况下,也可以通过父类名称访问静态类:

var text = MyWebservice.Helper.getText()

And finally, I created a simple website that demonstrates this and posted it to google docs . 最后,我创建了一个简单的网站来演示这一点,并将其发布到google docs上

EDIT II: your last edit shows that you have actually a web application , not a web site. 编辑II:您的上一次编辑显示您实际上有一个Web应用程序 ,而不是一个网站。 For web applications App_Code folder is not necessary. 对于Web应用程序,不需要App_Code文件夹。 In a web site, class files are compiled dynamically at run-time and must live in the App_Code folder. 在网站上,类文件在运行时动态编译,并且必须位于App_Code文件夹中。 In a web application, everything is compiled statically and class files can live anywhere in your web application. 在Web应用程序中,所有内容都是静态编译的,并且类文件可以存在于Web应用程序中的任何位置。

In that case I see the reason of this behavior in incorrect Build Action of the code files in your folder. 在那种情况下,我会在文件夹中代码文件的错误Build Action中看到此行为的原因。 I'm guessing it set to Content but should be set to Compile instead (you can do it by pressing F4 on your code file). 我猜它设置为Content但是应该设置为Compile (可以通过在代码文件上按F4来实现)。

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

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