asp.net ajax Advance Tips & Tricks (7) asp.net ajax with urlrewriting

Source: Internet
Author: User
Tags httpcontext

Objective:

Recent friends who use urlrewriting sometimes encounter asp.net Ajax and AJAX Control Toolkit controls that are not working properly, and are tricky for developers with no relevant experience. This article discusses some of the compatibility issues that should be noted when using ASP.net AJAX and urlrewriting through case studies and relative solutions.

Problem Recurrence:

A simple urlrewriting application is a path redirection from client to resource request, which is typically written like the following code 1 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 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);

Code 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);
}
}

However, when we use the above code for urlrewriting and use ASP.net ajax or AJAX Control Toolkit in the page, we get the ' sys ' is undefined error warning, and the AJAX controls are not working properly, Totally not work.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.