Preface:
Recently, some friends who use urlrewriting sometimes encounter the problem that ASP. NET Ajax and Ajax Control Toolkit controls cannot work normally, which is quite tricky for developers who have no relevant experience. This article uses case studies and Relative Solutions to discuss some compatibility issues that should be noted when ASP. NET Ajax and urlrewriting are used.
Problem reproduction:
Generally, simple urlrewriting applications redirect requests from the client to the resource path. The typical method is as follows:Code1 and Code 2:
Code 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 shocould show contents relevant
// The ID that is passed here
String Stargeturl = " ~ /Kategori. aspx? Category = " + Sid;
// Owing to rewritepath, the user will see requested URL in
// Address Bar
// The second argument shocould be false, to keep your references
// To images, CSS files
Context. rewritepath (stargeturl, False );
Code 2:
// In global. asax
Void Application_beginrequest ( Object Sender, eventargs E)
{
System. Web. 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 );
}
}
However, when we use the above Code for urlrewriting and ASP. NET Ajax or Ajax Control Toolkit on the page, we will get:'Sys 'is undefinedError warning, while the Ajax control cannot work normally, totally not work.
Analysis:
Obviously, 'sys 'is undefined tells us that the ASP. NET Ajax framework is not loaded normally. According to the analysis, sys is defined in the script resource fileScriptresource. axdBut in the above urlrewriting code, we do notScriptresource. axdAfter special processing, the resource is redirected and not loaded properly. Therefore, ASP. NET Ajax does not work.
Solution:
To understand the cause, we need to modify the urlrewriting code to add an exception to scriptresource. axd:
Solution of code 1:
Add special cases in checkpath
Private Static Void Checkpath ( String Path)
{
If ( ! String . Equals (path, virtualpathutility. toabsolute ( " ~ /Scriptresource. axd " ), Stringcomparison. ordinalignorecase ))
// It shoshould be at the root.
{
Throw404 ();
}
}
Solution of Code 2:
Similarly to 1, add a special case in Io judgment and modify it as follows:
Void Application_beginrequest ( Object Sender, eventargs E)
{
System. Web. 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 );
}
}
So far, this problem can be solved.
Related case:
Http://forums.asp.net/t/1178119.aspx
Http://forums.asp.net/p/1356089/2795441.aspx#2795441
Thanks.