这个问题之所以存在,是因为它确实存在 具有历史意义,但事实并非如此 被认为是一个很好的主题问题 因为是本网站,所以请不要使用 作为证据,你可以问类似的问题 这里的问题。 更多信息:https://stackoverflow.com/faq
总有一些功能在边缘场景中很有用,但正是因为这个原因,大多数人都不知道它们。我要求的是课本上通常没有教过的特性。
你知道的是什么?
这个问题之所以存在,是因为它确实存在 具有历史意义,但事实并非如此 被认为是一个很好的主题问题 因为是本网站,所以请不要使用 作为证据,你可以问类似的问题 这里的问题。 更多信息:https://stackoverflow.com/faq
总有一些功能在边缘场景中很有用,但正是因为这个原因,大多数人都不知道它们。我要求的是课本上通常没有教过的特性。
你知道的是什么?
当前回答
模板化用户控件。一旦你知道它们是如何工作的,你就会看到各种各样的可能性。下面是最简单的实现:
TemplatedControl.ascx
这里最棒的事情是使用简单而熟悉的用户控件构建块,并能够使用HTML和一些占位符来布局UI的不同部分。
<%@ Control Language="C#" CodeFile="TemplatedControl.ascx.cs" Inherits="TemplatedControl" %>
<div class="header">
<asp:PlaceHolder ID="HeaderPlaceHolder" runat="server" />
</div>
<div class="body">
<asp:PlaceHolder ID="BodyPlaceHolder" runat="server" />
</div>
TemplatedControl.ascx.cs
这里的“秘密”是使用ITemplate类型的公共属性,并知道[ParseChildren]和[PersistenceMode]属性。
using System.Web.UI;
[ParseChildren(true)]
public partial class TemplatedControl : System.Web.UI.UserControl
{
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Header { get; set; }
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate Body { get; set; }
void Page_Init()
{
if (Header != null)
Header.InstantiateIn(HeaderPlaceHolder);
if (Body != null)
Body.InstantiateIn(BodyPlaceHolder);
}
}
default . aspx
<%@ Register TagPrefix="uc" TagName="TemplatedControl" Src="TemplatedControl.ascx" %>
<uc:TemplatedControl runat="server">
<Header>Lorem ipsum</Header>
<Body>
// You can add literal text, HTML and server controls to the templates
<p>Hello <asp:Label runat="server" Text="world" />!</p>
</Body>
</uc:TemplatedControl>
你甚至会得到内部模板属性的智能感知。因此,如果您在团队中工作,您可以快速创建可重用的UI,以实现与您的团队已经从内置ASP中享受到的相同的可组合性。NET服务器控件。
MSDN示例(与开头的链接相同)添加了一些额外的控件和命名容器,但只有当您希望支持“中继器类型”控件时,才需要这样做。
其他回答
包含在ASP中。Net 3.5 sp1:
customErrors现在支持“redirectMode”属性,值为“ResponseRewrite”。显示错误页面而不改变URL。 表单标记现在识别操作属性。非常适合在使用URL重写时使用
HttpContext.Current will always give you access to the current context's Request/Response/etc., even when you don't have access to the Page's properties (e.g., from a loosely-coupled helper class). You can continue executing code on the same page after redirecting the user to another one by calling Response.Redirect(url, false ) You don't need .ASPX files if all you want is a compiled Page (or any IHttpHandler). Just set the path and HTTP methods to point to the class in the <httpHandlers> element in the web.config file. A Page object can be retrieved from an .ASPX file programmatically by calling PageParser.GetCompiledPageInstance(virtualPath,aspxFileName,Context)
EnsureChildControls方法:它检查子控件是否被初始化。如果子控件未初始化,则调用CreateChildControls方法。
我曾经开发过一个asp.net应用程序,它通过了一家领先的安全公司的安全审计,我学会了这个简单的技巧来防止一个不太为人所知但很重要的安全漏洞。
以下解释来自: http://www.guidanceshare.com/wiki/ASP.NET_2.0_Security_Guidelines_-_Parameter_Manipulation#Consider_Using_Page.ViewStateUserKey_to_Counter_One-Click_Attacks
考虑使用Page。ViewStateUserKey用于对抗一键式攻击。如果您对调用者进行身份验证并使用ViewState,请设置Page。Page_Init事件处理程序中的ViewStateUserKey属性,以防止一键式攻击。
void Page_Init (object sender, EventArgs e) {
ViewStateUserKey = Session.SessionID;
}
将属性设置为您知道对每个用户都是唯一的值,例如会话ID、用户名或用户标识符。
A one-click attack occurs when an attacker creates a Web page (.htm or .aspx) that contains a hidden form field named __VIEWSTATE that is already filled with ViewState data. The ViewState can be generated from a page that the attacker had previously created, such as a shopping cart page with 100 items. The attacker lures an unsuspecting user into browsing to the page, and then the attacker causes the page to be sent to the server where the ViewState is valid. The server has no way of knowing that the ViewState originated from the attacker. ViewState validation and HMACs do not counter this attack because the ViewState is valid and the page is executed under the security context of the user.
通过设置ViewStateUserKey属性,当攻击者浏览到一个页面以创建ViewState时,该属性将初始化为攻击者的名字。当合法用户向服务器提交页面时,将使用攻击者的名称对页面进行初始化。结果,ViewState HMAC检查失败并生成异常。
HttpContext。IsCustomErrorEnabled是一个很酷的特性。我不止一次发现它很有用。这里有一篇关于它的短文。