国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET AJAX Advance Tips & Tricks (7) ASP.NET AJAX 與 URLRewriting

2019-11-10 22:34:59
字體:
來源:轉載
供稿:網友

前言:

最近一些使用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.

分析:

很顯然,'sys' is undefined 告訴我們 ASP.NET AJAX框架沒有正常加載,根據分析可知,sys的定義是在腳本資源文件ScriptResource.axd中的,然而在上面的URLRewriting代碼中,我們并沒有對ScriptResource.axd進行特殊處理,導致該資源被重定向,沒有正常加載。所以導致了ASP.NET AJAX不工作的問題。

解決方法:

知道了原因,我們就要修改URLRewriting的代碼,為ScriptResource.axd增加一個例外:

代碼1的解決方案:

在CheckPath時加入特例

復制代碼private static void CheckPath(string path){    if (!string.Equals(path, VirtualPathUtility.ToAbsolute("~/ScriptResource.axd"), StringComparison.OrdinalIgnoreCase))    //it should be at the root.    {        Throw404();    }}復制代碼

 

代碼2的解決方案:

與1同理,在IO判斷時加上特例,修改如下:

復制代碼    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") && !currentURL.EndsWith("ScriptResource.axd"))        {            string reWritePage = "ViewProfile.aspx";            httpContext.RewritePath(reWritePage);        }    } 復制代碼

 

至此,可解決該問題。

相關case:

http://forums.asp.net/t/1178119.aspx

http://forums.asp.net/p/1356089/2795441.aspx#2795441

Thanks.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 增城市| 民勤县| 晋中市| 通辽市| 吴桥县| 子长县| 华亭县| 自治县| 锡林郭勒盟| 余姚市| 灵石县| 罗城| 奈曼旗| 敦化市| 昌邑市| 车致| 鞍山市| 灵丘县| 侯马市| 建宁县| 湘潭县| 永康市| 通辽市| 景德镇市| 长治市| 敦化市| 宝丰县| 金昌市| 朔州市| 阿拉善左旗| 确山县| 布拖县| 昌平区| 潜江市| 徐闻县| 萍乡市| 大冶市| 邯郸市| 年辖:市辖区| 东至县| 政和县|