所以我有两个功能,我遇到了一个有趣的问题。本质上,我的目标是让我的代码在一个易于包含的 cs 文件中更具可移植性。
这是说cs文件:
namespace basicFunctions {
public partial class phpPort : System.Web.UI.Page {
public 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();
}
}
}
这是引用它的页面:
<% @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(basicFunctions.phpPort.includer("header.html"));
//irrelevant code
if ('stuff happens') {
basicFunctions.phpPort.returnError();
}
Response.Write(basicFunctions.phpPort.includer("footer.html"));
}
</script>
我得到的错误是上面列出的错误,即:
Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'basicFunctions.phpPort.includer(string)'