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

首頁 > 學院 > 開發(fā)設計 > 正文

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

2019-11-10 21:48:39
字體:
來源:轉載
供稿:網(wǎng)友

前言:

最近一些使用URLRewriting的朋友們有時候會遇到asp.net Ajax和AJAX Control Toolkit控件不能正常工作的現(xiàn)象,對于沒有相關經(jīng)驗的開發(fā)者來說相當棘手。本篇通過案例分析和相對的解決方案來討論在使用ASP.NET AJAX 與 URLRewriting 時應當注意到的一些兼容性問題。

問題重現(xiàn):

一般簡單的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框架沒有正常加載,根據(jù)分析可知,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.


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 贵德县| 肇州县| 万宁市| 体育| 郸城县| 黄平县| 纳雍县| 中卫市| 会同县| 阜新市| 新邵县| 陆良县| 西吉县| 西青区| 双峰县| 册亨县| 麻栗坡县| 通州市| 扬中市| 鄂托克前旗| 高州市| 长葛市| 梅州市| 东平县| 安泽县| 蓬溪县| 全椒县| 葫芦岛市| 汶川县| 黔东| 亳州市| 威海市| 简阳市| 湘阴县| 措美县| 新竹县| 灵石县| 舒兰市| 康保县| 大同县| 昔阳县|