这个问题之所以存在,是因为它确实存在 具有历史意义,但事实并非如此 被认为是一个很好的主题问题 因为是本网站,所以请不要使用 作为证据,你可以问类似的问题 这里的问题。 更多信息:https://stackoverflow.com/faq
总有一些功能在边缘场景中很有用,但正是因为这个原因,大多数人都不知道它们。我要求的是课本上通常没有教过的特性。
你知道的是什么?
这个问题之所以存在,是因为它确实存在 具有历史意义,但事实并非如此 被认为是一个很好的主题问题 因为是本网站,所以请不要使用 作为证据,你可以问类似的问题 这里的问题。 更多信息:https://stackoverflow.com/faq
总有一些功能在边缘场景中很有用,但正是因为这个原因,大多数人都不知道它们。我要求的是课本上通常没有教过的特性。
你知道的是什么?
当前回答
HttpContext。项作为请求级缓存工具
其他回答
WebMethods。
你可以使用ASP。NET AJAX回调到ASPX页面中的web方法。你可以用[WebMethod()]和[ScriptMethod()]属性来修饰一个静态方法。例如:
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static List<string> GetFruitBeginingWith(string letter)
{
List<string> products = new List<string>()
{
"Apple", "Banana", "Blackberry", "Blueberries", "Orange", "Mango", "Melon", "Peach"
};
return products.Where(p => p.StartsWith(letter)).ToList();
}
现在,在你的ASPX页面你可以这样做:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<input type="button" value="Get Fruit" onclick="GetFruit('B')" />
</div>
</form>
并通过JavaScript调用您的服务器端方法使用:
<script type="text/javascript">
function GetFruit(l)
{
PageMethods.GetFruitBeginingWith(l, OnGetFruitComplete);
}
function OnGetFruitComplete(result)
{
alert("You got fruit: " + result);
}
</script>
模板化用户控件。一旦你知道它们是如何工作的,你就会看到各种各样的可能性。下面是最简单的实现:
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示例(与开头的链接相同)添加了一些额外的控件和命名容器,但只有当您希望支持“中继器类型”控件时,才需要这样做。
EnsureChildControls方法:它检查子控件是否被初始化。如果子控件未初始化,则调用CreateChildControls方法。
HttpContext。IsCustomErrorEnabled是一个很酷的特性。我不止一次发现它很有用。这里有一篇关于它的短文。
这是最好的一个。把它加到你的网里。配置更快的编译。这是3.5SP1后通过这个QFE。
<compilation optimizeCompilations="true">
Quick summary: we are introducing a new optimizeCompilations switch in ASP.NET that can greatly improve the compilation speed in some scenarios. There are some catches, so read on for more details. This switch is currently available as a QFE for 3.5SP1, and will be part of VS 2010. The ASP.NET compilation system takes a very conservative approach which causes it to wipe out any previous work that it has done any time a ‘top level’ file changes. ‘Top level’ files include anything in bin and App_Code, as well as global.asax. While this works fine for small apps, it becomes nearly unusable for very large apps. E.g. a customer was running into a case where it was taking 10 minutes to refresh a page after making any change to a ‘bin’ assembly. To ease the pain, we added an ‘optimized’ compilation mode which takes a much less conservative approach to recompilation.
通过在这里: