Categories
ASP.NET C# Programming

Adding AJAX to an existing project that includes Routing

I was working on a project and just on a whim decided to try out some of .NET’s AJAX controls. I chose a random page, added a ScriptManager, tossed in a TextBox and a CalendarExtender.

F5. Page shows up. Javascript error:

Line: 121
Char: 1
Error: 'Sys' is undefined
Code: 0
Url: http://localhost:49329/admin.aspx

Hmm.

Try a new project. Add a ScriptManager, tossed in a TextBox and a CalendarExtender. Works fine. Huh?

I go through the Web.Config files of the two. Nothings changed except the version number on everything:

non working: Version=3.5.0.0
working: Version=1.0.61025.0

Ah ha! That must be it. I reset the working project as a 3.5 app and run it. I was so sure it was going to fail. Nope. Worked fine.

Must be something else. *grumble*

I start taking pieces out of my existing app and when I came to the Global.asax I took out the RouteTables and AJAX comes to life! FINALLY!

Here is what I had:

Global.asax

		protected void Application_Start(object sender, EventArgs e)
		{
			// Code that runs on application startup
			RegisterRoutes(RouteTable.Routes);
		}
		private static void RegisterRoutes(ICollection<RouteBase> Routes)
		{
			RouteTable.Routes.Add(
				new Route("{Parameter}/{pageID}", new RouteHandler()));
			RouteTable.Routes.Add(
				new Route("{Parameter}", new RouteHandler()));
		}

With my routing handler file showing this little gem:

RouteHandler.cs

				string virtualPath = string.Format("~/{0}.aspx", pageName);
				return (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));

So basically it was swiping the file extensions from my httpHandlers and replacing them with .aspx meaning they were not found.

Web.Config

		<httpHandlers>
			<remove verb="*" path="*.asmx"/>
			<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
			<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
			<add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
		</httpHandlers>

To handle the situation I added the following to Global.asax:

Global.asax

		protected void Application_Start(object sender, EventArgs e)
		{
			// Code that runs on application startup
			RegisterRoutes(RouteTable.Routes);
		}
		private static void RegisterRoutes(ICollection<RouteBase> Routes)
		{
			RouteTable.Routes.Add(new System.Web.Routing.Route("{resource}.axd/{*pathInfo}",
					   new System.Web.Routing.StopRoutingHandler()));
			RouteTable.Routes.Add(new System.Web.Routing.Route("{service}.asmx/{*path}",
					   new System.Web.Routing.StopRoutingHandler()));
			RouteTable.Routes.Add(
				new Route("{Parameter}/{pageID}", new RouteHandler()));
			RouteTable.Routes.Add(
				new Route("{Parameter}", new RouteHandler()));
		}

I hope this helps. 🙂