gpt4 book ai didi

asp.net - ASP.NET 中的基页

转载 作者:行者123 更新时间:2023-12-04 01:52:44 25 4
gpt4 key购买 nike

您是否建议在 Visual Studio 中创建的每个网站都应该创建一个用作父类的基页?

确切的好处/缺点是什么?

最佳答案

如果您想覆盖 ASP.NET 中某些内容的工作方式,将其构建到基类中比将代码包含在每个页面中会更有效。我这样做的两个具体实例是:

IsPostback
鲜为人知的事实:制作一个对于 ASP.NET 来说看起来像回发但通过 GET 请求提交的请求是非常可行的。谁做这个?黑客,就是这样。调用 IsPostback在这种情况下将返回 true ,但它真的应该返回 false .为了解决这个问题,构建一个覆盖 IsPostBack 的基类:

Public Class MyBase
Inherits System.Web.UI.Page
<DebuggerStepThrough()> _
Public Shadows Function IsPostback() As Boolean

'Check the built-in IsPostback and make sure this is a HTTP POST
Return (Page.IsPostBack AndAlso Request.HttpMethod.ToUpper = "POST")

End Function

End Class

错误处理
Beginning ASP.NET Security , Blowdart谈到这样一个事实:如果您使用 ASP.NET 的错误处理将客户端重定向到自定义错误页面,黑客(再次)可以检测到重定向并将其标记为可能被利用的错误。更安全的模式是处理页面的错误事件并执行 Server.Transfer(不向客户端发送任何内容)。同样,在基类中执行此操作意味着您只需编写一次代码:

public partial MyBase : System.Web.UI.Page
{
protected void Page_Error (object sender, EventArgs e)
{
Exception ex = Server.GetLastError();

// Do something with the exception e.g. log it
...

Server.Transfer("~/mycustomerrorpage.aspx");
}
}

关于asp.net - ASP.NET 中的基页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com