ASPX 页面中的公共方法在其他脚本中使用
本周我们的服务器上遇到了一个非常奇怪的问题,其中 aspx 脚本(没有代码隐藏文件)中的一个方法以某种方式从我们网站上其他完全不相关的页面调用。基本上,该脚本是这样说的:
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
// some initializer logic
PageMethod("data");
}
public string PageMethod (string strInput)
{
// More logic
}
</script>
我们运营着一个由四台服务器组成的场,并且我们看到 PageMethod
在所有服务器上的多个位置被脚本调用,所以虽然我的第一直觉是这是一个损坏的在服务器上缓存(我仍然怀疑它可能是),我的直觉告诉我,我们允许这种情况在我们的代码中发生。我们一直在使用这种代码风格,但最近将我们的服务器升级到.NET 2.0。
我的第一个问题是,aspx 中的 public
方法是否可以被网站上其他地方的脚本访问?我的第二个问题是,我们应该在 aspx 页面上使用 private
还是 protected
方法(我一直认为独立页面中的代码是自包含的,因此访问不顺畅)修饰符不应该产生影响)。
(注意:我写的c#不多,所以我对这门语言的掌握几乎只是理论上的。我只是想在这个问题成为真正的问题之前解决它。)
We had a very strange issue on our servers this week, where a method in an aspx script (with no code-behind file) was somehow getting called from completely unrelated pages elsewhere on our site. Basically, the script said this:
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
// some initializer logic
PageMethod("data");
}
public string PageMethod (string strInput)
{
// More logic
}
</script>
We operate a farm of four servers, and we were seeing the PageMethod
getting called by scripts in several places on all servers, so while my first instinct was that this was a corrupted cache on the server (and I still suspect it might be), my gut tells me that we were allowing this to happen with our code. We've been using this code style for yonks, but recently upgraded our servers to .NET 2.0.
My first question is, should a public
method in an aspx every be accessible to a script elsewhere on a website? And my second question is, should we be using private
or protected
methods on aspx pages (I always figured the code in a stand-alone page was self contained, so sloppy access modifiers shouldn't make a difference).
(Note: I don't write much c#, so my grasp of the language is pretty much just theoretical. I'm just trying to troubleshoot this issue before it becomes a real issue.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了调用公共实例方法,需要此类的实例。我怀疑您的问题与直接调用此方法的其他脚本无关。更有可能的是,有一个 HTTP 请求被发送到相应的 ASPX 页面,该页面又调用该方法。此请求可以由客户端(页面上的某些链接...)或直接由服务器(使用 HttpWebRequest)完成。要进一步调试问题,请尝试查看 Web 服务器日志文件以查看对此页面发出的所有请求。
如果这些方法不打算被代码的其他部分使用,那么最好将它们设置为私有或受保护,这不仅与 ASP.NET 有关。
In order to invoke a public instance method, an instance of this class is required. I suspect that your issue is not related to other scripts calling this method directly. It's more probable that there is an HTTP request being sent to the corresponding ASPX page which in turns invokes the method. This request could be done by the client (some link on a page, ...) or directly by the server (using HttpWebRequest). To further debug the problem try looking at the web server log files to see all the requests being made to this page.
If those method are not intended to be used by other parts of the code it is a good practice to make them private or protected and that's not only related to ASP.NET.
最好将 PageMethod 之类的方法设置为私有或受保护。我相信 Page_Load 和其他页面生命周期方法必须是公开的,但其余方法则不然。 asp.net 生成的类可以像任何其他类一样访问,尽管有时它们很难找到。我通常看到这样奇怪的情况是,有人复制了一个 aspx 页面,但忽略了更改页面指令,因此前面有多个代码指向单个后面的代码。
It's probably best to make methods like PageMethod private or protected. The Page_Load and other page lifecycle methods have to be public I believe, but the rest do not. The classes that asp.net generates can be accessed like any other class, though sometimes they're tricky to find. Where I usually see weirdness like that is when someone has copied an aspx page but neglected to change the page directive, so that you have multiple code in fronts pointing at a single code-behind.