首页 / 脚本样式 / Ajax / ASP.NET AJAX Advance Tips & Tricks (7) ASP.NET AJAX与URLRewriting
ASP.NET AJAX Advance Tips & Tricks (7) ASP.NET AJAX与URLRewriting2011-10-18 cnblogs Lance Zhang前言:最近一些使用URLRewriting的朋友们有时候会遇到ASP.NET AJAX和AJAX Control Toolkit控件不能正 常工作的现象,对于没有相关经验的开发者来说相当棘手。本篇通过案例分析和相对的解决方案来讨论在 使用ASP.NET AJAX 与 URLRewriting 时应当注意到的一些兼容性问题。问题重现:一般简单的URLRewriting应用都是对来自客户端对资源的Request进行路径重定向,比较典型的写法如 同下列代码1 和代码2:代码1:// If the requested file exists if (File.Exists(Request.PhysicalPath)) { // Do nothing here, just serve the file } // If the file does not exist then else if (!File.Exists(Request.PhysicalPath)) { // Get the URL requested by the user string sRequestedURL = Request.Path.Replace(".aspx", "").Replace ("/gratis24/",""); // You can retrieve the ID of the content from database that is // relevant to this requested URL (as per your business logic) string sId; sId = GetContentIDByPath(sRequestedURL); // The ShowContents.aspx page should show contents relevant to // the ID that is passed here string sTargetURL = "~/Kategori.aspx?category=" + sId; // Owing to RewritePath, the user will see requested URL in the // address bar // The second argument should be false, to keep your references // to images, css files Context.RewritePath(sTargetURL, false);代码2://in global.asax void Application_BeginRequest(object sender, EventArgs e) { System.Web.HttpContext httpContext = HttpContext.Current; String currentURL = httpContext.Request.Path.ToLower(); //Creates the physical path on the server string physicalPath = httpContext.Server.MapPath(currentURL.Substring (currentURL.LastIndexOf("/") + 1)); //checks to see if the file does not exsists. if (!System.IO.File.Exists(physicalPath) && !currentURL.EndsWith ("webresource.axd")) { string reWritePage = "ViewProfile.aspx"; httpContext.RewritePath(reWritePage); } }然而,当我们使用上面的代码进行URLRewriting,并且在页面中使用ASP.NET AJAX 或 AJAX Control Toolkit的话,将会得到:"sys" is undefined 错误警告,而AJAX控件不能正常工作,totally not work.