摘要:在博客园看到一文,是关于Login控件在UpdatePanel里重新定向时发生的问题的。其实,重新定向是如此基本的东西,ASP.NET AJAX怎么会不支持呢?在Reflector里,如果仔细看一下ScriptModule实现的细节,它对PreSendRequestHeaders事件做了处理 protected virtual void Init(HttpApplication context){context.PreSendRequestHeaders += new EventHandler(this.PreSendRequestHeadersHandler);context.PostAcquireRequestState += new EventHandler(this.OnPostAcquireRequestState);context.AuthenticateRequest += new EventHandler(this.AuthenticateRequestHandler);} private void PreSendRequestHeadersHandler(object sender, EventArgs args){HttpApplication application1 = (HttpApplication) sender;if (PageRequestManager.IsAsyncPostBackRequest(application1.Request.Headers)){HttpResponse response1 = application1.Response;if (response1.StatusCode == 0x12e){string text1 = response1.RedirectLocation;List<HttpCookie> list1 = new List<HttpCookie>(response1.Cookies.Count);for (int num1 = 0; num1 < response1.Cookies.Count; num1++){list1.Add(response1.Cookies[num1]);}response1.ClearContent();response1.ClearHeaders();for (int num2 = 0; num2 < list1.Count; num2++){response1.AppendCookie(list1[num2]);}response1.Cache.SetCacheability(HttpCacheability.NoCache);response1.ContentType = "text/plain"response1.Write(PageRequestManager.EncodeString("pageRedirect", string.Empty, text1));}}else if (RestHandlerFactory.IsRestRequest(application1.Context) && (application1.Response.StatusCode == 0x12e)){RestHandler.WriteExceptionJsonString(application1.Context, new InvalidOperationException(AtlasWeb.WebService_RedirectError));}} 正是处理重新定向(StatusCode=302)的,如果在web.config里设置了(这是使用 ASP.NET AJAX-Enabled Web Site 项目模板的默认设置): <httpModules> ..... <add name="ScriptModule" type="Microsoft.Web.UI.ScriptModule, Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/></httpModules> 使用一个类似HttpWatch/Fiddler这样的工具,在使用UpdatePanel的情形下,针对服务器端重新定向,你会看到类似这样的输出: 23|pageRedirect||/AJAXTest2/Default.aspx|
为了确认,把上述的httpModules里的<add>注释掉的话,确实得到博客园原文里的同样错误。
所以,如果你是给现有ASP.NET项目里添加AJAX支持的话,先用ASP.NET AJAX-Enabled Web Site项目模板生成一个空项目,然后把对应的web.config里的设置拷贝到现有的web.config里,这是比较保险的做法。 ...[
阅读全文]