简体   繁体   中英

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.

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:

<%@ 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 .

Go to your App_Code folder, and select your class. Look in the properties page. Find Build Action . Set it to 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:

static class MyClass
{
}

If you declared the class in such a way, it will have internal access modifier. 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.

EDIT: one more - there is no difference whether the class declared as static or not in this case. 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 .

EDIT II: your last edit shows that you have actually a web application , not a web site. For web applications App_Code folder is not necessary. In a web site, class files are compiled dynamically at run-time and must live in the App_Code folder. In a web application, everything is compiled statically and class files can live anywhere in your web application.

In that case I see the reason of this behavior in incorrect Build Action of the code files in your folder. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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