An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'

StackOverflow https://stackoverflow.com/questions/10505187

Question

So I have two functions and I'm getting an interesting problem. Essentially I'm aiming to make my code more portable in an easily includeable cs file.

Here's said cs file:

namespace basicFunctions {
public partial class phpPort : System.Web.UI.Page {
    public static string includer(string filename) {
        string path = Server.MapPath("./" + filename);
        string content = System.IO.File.ReadAllText(path);
        return content;
    }
    public void returnError() {
        Response.Write("<h2>An error has occurred!</h2>");
        Response.Write("<p>You have followed an incorrect link. Please double check and try again.</p>");
        Response.Write(includer("footer.html"));
        Response.End();
    }
}
}

Here is the page that is referencing it:

<% @Page Language="C#" Debug="true" Inherits="basicFunctions.phpPort" CodeFile="basicfunctions.cs" %>
<% @Import Namespace="System.Web.Configuration" %>

<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
    Response.Write(includer("header.html"));
    //irrelevant code
    if ('stuff happens') {
        returnError();
    }
    Response.Write(includer("footer.html"));
}
</script>

The error I'm getting is the one listed above, namely:

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'

On the following line:

Line 5: string path = Server.MapPath("./" + filename);

Was it helpful?

Solution

Server is only available to instances of System.Web.UI.Page-implementations (as it's an instance property).

You have 2 options:

  1. Convert the method from static to instance
  2. Use following code:

(overhead of creating a System.Web.UI.HtmlControls.HtmlGenericControl)

public static string FooMethod(string path)
{
    var htmlGenericControl = new System.Web.UI.HtmlControls.HtmlGenericControl();
    var mappedPath = htmlGenericControl.MapPath(path);
    return mappedPath;
}

or (not tested):

public static string FooMethod(string path)
{
    var mappedPath = HostingEnvironment.MapPath(path);
    return mappedPath;
}

or (not that good option, as it somehow fakes to be static but rather is static for webcontext-calls only):

public static string FooMethod(string path)
{
    var mappedPath = HttpContext.Current.Server.MapPath(path);
    return mappedPath;
}

OTHER TIPS

What about using HttpContext.Current? I think you can use that to get a reference to Server in a static function.

Described here: HttpContext.Current accessed in static classes

I ran into a similar thing some time back -- put simply you can't pull Server.MapPath() from the .cs code-behind inside a static method (unless that code behind somehow inherits a web page class, which is probably not allowed anyway).

My simple fix was to have the code behind method capture the path as an argument, then the calling web page executes the method with Server.MapPath during the call.

Code Behind (.CS):


public static void doStuff(string path, string desc)
{
    string oldConfigPath=path+"webconfig-"+desc+"-"+".xml";

... now go do something ...
}

Web-Page (.ASPX) Method Call:


...
doStuff(Server.MapPath("./log/"),"saveBasic");
...

No need to bash or talk down to the OP, it seemed a legitimate confusion. Hope this helps ...

public static string includer(string filename) 
{
        string content = System.IO.File.ReadAllText(filename);
        return content;
}


includer(Server.MapPath("./" + filename));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top