3

I am a newbie to asp.net field and having some problem in implementing url routing in asp.net 3.5 (I know it can be easily implemented in asp.net 4.0).

Here is what I have done.....

a) I am using .NET Framework 3.5 SP1.

b) Added System.Web.Routing assembly reference in web.config

<assemblies>
   <add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, 
PublicKeyToken=31BF3856AD364E35"/>
</assemblies>

c) Add the UrlRoutingModule HTTP Module

<httpModules>
<add name="RoutingModule" 
     type="System.Web.Routing.UrlRoutingModule, System.Web.Routing,
         Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

d) Code in Global.asax

 void Application_Start(object sender, EventArgs e) 
 {

    RegisterRoutes();
 }

 private static void RegisterRoutes()
 {
    System.Web.Routing.RouteTable.Routes.Add(
            "SaveUser", new System.Web.Routing.Route("SaveUser",
                                new RouteHandler("~/Register.aspx")));

 }

e) RouteHandler.cs Class

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Routing;
 using System.Web.Compilation;
 using System.Web.UI;

 public class RouteHandler : IRouteHandler
 {
   public RouteHandler()
   {
   }

   public RouteHandler(string virtualPath)
   {
      _virtualPath = virtualPath;
   }

   public IHttpHandler GetHttpHandler(RequestContext requestContext)
   {
       //var display = BuildManager.CreateInstanceFromVirtualPath(
       //                _virtualPath, typeof(Page)) as IDisplay;
       var abc = BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page)) as IDisplay;
       return abc;
   }

   string _virtualPath;
}

f) Code in Default.aspx.cs

 using System;
 using System.Configuration;
 using System.Data;
 using System.Linq;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.HtmlControls;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Xml.Linq;

 public partial class _Default : System.Web.UI.Page 
 {
   protected void Page_Load(object sender, EventArgs e)
   {
      Response.Redirect("~/SaveUser");
   }
 }

and I am getting the following error

The route handler 'RouteHandler' did not return an IHttpHandler from its GetHttpHandler() method.

I tried 100 of links but could not make out what is wrong.

If anyone have experience in url routing in asp.net 3.5.....pls help.... I need to implement it very urgently....

Thanks in advance....

1
  • 2
    What's Default2.aspx look like?
    – bzlm
    Dec 24, 2011 at 11:23

3 Answers 3

1
System.Web.Routing 

only available on .net framework version 4

1
0

This is probably too little, too late, but I recently upgraded a .NET 3.5 ASP.NET site to use routing and I see your issue.

The problem is that when you configure the routing module, it creates a 2nd context for the routing module that is completely separate from HttpContext.Current in the ASP.NET page. So, you need to set up your handler so that you can access this other context instance (which happens to be a RequestContext).

public class RouteHandler : IRouteHandler
{
    public RouteHandler()
    {
    }

    public RouteHandler(string virtualPath)
    {
      _virtualPath = virtualPath;
    }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        //Record the request context of the routing module in HttpContext.Current, so we can use it in pages.
        HttpContext.Current.Items("requestContext") = requestContext
        return BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(Page)) as IDisplay;
    }

    string _virtualPath;
}

Now, in the page, you need to access the context from HttpContext.Current.Items.

public partial class _Default : System.Web.UI.Page 
{
    private readonly RequestContext RequestContext
    {
        get { return (RequestContext)HttpContext.Current.Items("requestContext"); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        RequestContext.HttpContext.Response.Redirect("~/SaveUser");
    }
}
0

I've been struggling with this same problem, and here's one thing I've learned. On the page referenced by Rick Schott, it says that what you put in web.config depends on what version of IIS you're deploying to. Use this for IIS 6, or IIS 7 in "classic mode":

<httpModules>
  <add name="UrlRoutingModule" 
       type="System.Web.Routing.UrlRoutingModule, 
             System.Web.Routing, 
             Version=3.5.0.0, 
             Culture=neutral, 
             PublicKeyToken=31BF3856AD364E35"/>
</httpModules>

...but in IIS 7+ "integrated mode", add this instead:

<system.webServer>
  <modules>
    <remove name="UrlRoutingModule" />
    <add name="UrlRoutingModule" 
         type="System.Web.Routing.UrlRoutingModule, 
               System.Web.Routing, 
               Version=3.5.0.0, 
               Culture=neutral, 
               PublicKeyToken=31BF3856AD364E35"/>
  </modules>
</system.webServer>

One important thing I've discovered is that though the latter may be what's correct for your IIS, the former is what works inside Visual Studio 2008. So you may have to deploy a different web.config from the one you develop with.

Another useful fact I've found which isn't well documented is that the path pattern you feed to the Route constructor is app-relative, not host-relative, and it should not start with a leading "/" or "~/". Just start with the first subfolder name, or page name if at app root level.

You have to get all that right just to enable it to invoke your IRouteHandler. Then you can worry about the two-contexts issue, if any. I had no trouble writing to requestContext.HttpContext.Items in the handler class and then reading from Context.Items in the target page.

But since you got the error message "did not return an IHttpHandler from its GetHttpHandler()", it sounds like you've got that working at least on your desktop. So the question then is... why are you casting your page instance as IDisplay instead of as IHttpHandler? That seems like the obvious first thing to change. I tried casting the return as Page and it seems to just want a direct cast to IHttpHandler instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.