C# 非静态字段、方法或属性“System.Web.UI.Page.Server.get”需要对象引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10505187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'
提问by
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.
所以我有两个功能,我遇到了一个有趣的问题。本质上,我的目标是使我的代码在易于包含的 cs 文件中更具可移植性。
Here's said cs file:
这是说的cs文件:
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'
编译器错误消息:CS0120:非静态字段、方法或属性“System.Web.UI.Page.Server.get”需要对象引用
On the following line:
在以下行:
Line 5: string path = Server.MapPath("./" + filename);
第 5 行:字符串路径 = Server.MapPath("./" + 文件名);
采纳答案by Andreas Niedermair
Serveris only available to instances of System.Web.UI.Page-implementations (as it's an instance property).
Server仅适用于System.Web.UI.Page-implementations 的实例(因为它是一个实例属性)。
You have 2 options:
您有 2 个选择:
- Convert the method from static to instance
- Use following code:
- 将方法从静态转换为实例
- 使用以下代码:
(overhead of creating a System.Web.UI.HtmlControls.HtmlGenericControl)
(创建一个的开销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):
或者(不是那么好的选择,因为它以某种方式假装是静态的,而是仅对于 webcontext-calls 是静态的):
public static string FooMethod(string path)
{
var mappedPath = HttpContext.Current.Server.MapPath(path);
return mappedPath;
}
回答by Nilish
public static string includer(string filename)
{
string content = System.IO.File.ReadAllText(filename);
return content;
}
includer(Server.MapPath("./" + filename));
回答by SouthShoreAK
What about using HttpContext.Current? I think you can use that to get a reference to Serverin a static function.
怎么用HttpContext.Current?我认为您可以使用它来获取对Server静态函数的引用。
Described here: HttpContext.Current accessed in static classes
回答by Chris
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).
前段时间我遇到了类似的事情——简单地说,你不能从静态方法中的 .cs 代码隐藏中提取 Server.MapPath()(除非后面的代码以某种方式继承了一个网页类,这可能不是无论如何都允许)。
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.
我的简单修复是让方法背后的代码捕获路径作为参数,然后调用网页在调用期间使用 Server.MapPath 执行该方法。
Code Behind (.CS):
代码隐藏(.CS):
public static void doStuff(string path, string desc)
{
string oldConfigPath=path+"webconfig-"+desc+"-"+".xml";
... now go do something ...
}
Web-Page (.ASPX) Method Call:
网页 (.ASPX) 方法调用:
...
doStuff(Server.MapPath("./log/"),"saveBasic");
...
No need to bash or talk down to the OP, it seemed a legitimate confusion. Hope this helps ...
无需抨击或与 OP 对话,这似乎是一种合理的混淆。希望这可以帮助 ...

