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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

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

2019-11-10 22:51:23
字體:
供稿:網(wǎng)友

前言:

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

問題重現(xiàn):

一般簡(jiǎn)單的URLRewriting應(yīng)用都是對(duì)來自客戶端對(duì)資源的Request進(jìn)行路徑重定向,比較典型的寫法如同下列代碼1 和代碼2:

代碼1:

復(fù)制代碼        // 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);復(fù)制代碼

 

代碼2:

復(fù)制代碼//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);        }                 } 復(fù)制代碼

 

然而,當(dāng)我們使用上面的代碼進(jìn)行URLRewriting,并且在頁面中使用ASP.NET AJAX 或 AJAX Control Toolkit的話,將會(huì)得到:'sys' is undefined 錯(cuò)誤警告,而AJAX控件不能正常工作,totally not work.

分析:

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

解決方法:

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

代碼1的解決方案:

在CheckPath時(shí)加入特例

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

 

代碼2的解決方案:

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

復(fù)制代碼    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);        }    } 復(fù)制代碼

 

至此,可解決該問題。

相關(guān)case:

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

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

Thanks.


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 房产| 衡南县| 台山市| 基隆市| 中牟县| 通州区| 南岸区| 宜兰县| 宁海县| 利川市| 丰台区| 徐水县| 亳州市| 东乌珠穆沁旗| 万盛区| 报价| 邵东县| 寿阳县| 绥江县| 台东县| 洛南县| 康定县| 兰考县| 辉县市| 鸡泽县| 邹平县| 尉氏县| 从化市| 柏乡县| 东安县| 广西| 海盐县| 阿拉善盟| 修武县| 贵溪市| 滨州市| 洛南县| 乌拉特后旗| 阿坝| 马鞍山市| 玉树县|