Url Routing in ASP.NET 3.5 breaks AJAX

  • Thread starter Thread starter Dmitri Vaganov
  • Start date Start date
this happens when you have wildcarding in place
one way to solve this to add a constraint to your routes so they only work
for .aspx , ... files - something like this :

RouteValueDictionary constraints = new RouteValueDictionary( new { Page =
@".*\.aspx*$" } );
Route rt = new Route( "{Page}" , RouteHandler );
rt.Constraints = constraints;
routes.Add( "RouteHandler" , rt );
 
Thanks for your help, Gerry. When I add the constraint, the Routing stops
redirecting to the page that I specified. When I remove constraint it works
fine.

Here is my routing code:

RouteValueDictionary constraints = new RouteValueDictionary(new
{
Page =
@".*\.aspx*$"
});
Route rt = new Route("{Page}", new
CustomRouteHandler("~/WelcomePage.aspx"));
rt.Constraints = constraints;
routes.Add("RouteHandler", rt);

Here is CustomRouteHandler:

public class CustomRouteHandler : IRouteHandler
{
public CustomRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}

public string VirtualPath { get; private set; }

public IHttpHandler GetHttpHandler(RequestContext
requestContext)
{
foreach (var urlParm in requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;
}
var page = BuildManager.CreateInstanceFromVirtualPath
(VirtualPath, typeof(Page)) as IHttpHandler;

return page;
}
}
 
Gerry, I added a custom constraint class to catch the pages with "." in the
name. That fixed the issue. Thanks for all your help!!!
 
Back
Top