5

I am trying to create a route in route table that routes to a virtual item (using a cms that creates url like example.com/about/company, where there is no physical file called company exists) using system.web.routing (unfortunately i cannot use iis rewriting/routing). I have tried the following but it results in 404. If I were to point to another physical file (tor testing purpose), the routing works fine.

void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = true;
    routes.MapPageRoute("about", "about/us", "~/about/company", false);
}

So, is it possible to point to an item like that?

jdphenix
  • 15,022
  • 3
  • 41
  • 74
xoail
  • 2,978
  • 5
  • 36
  • 70

3 Answers3

1

You will not be able to accomplish your desired goal with MapPageRoute. MapPageRoute works by creating a new Route with a PageRouteHandler. The documentation for the PageRouteHandler Class specifically states:

You pass an instance of PageRouteHandler to the Route constructor in order to map a URL of a physical file. The PageRouteHandler object specifies the virtual path of the physical file and determines whether authorization rules for the physical URL is checked.

If this is an MVC application there are extensions for routing in the RouteCollectionExtensions Class. You should be able to use the MapRoute method and feed it the necessary default parameters to create new Route with an MvcRouteHandler like so:

routes.MapRoute("about", "about/us", new { controller = "about", action="company", id="" });

You'll need to put in the appropriate values since I don't know your controller design. Also, have a look at the Creating Custom Routes (C#), which may be helpful for understanding how MVC handles routing.

If this is not an MVC app, you'll have to implement a custom route handler derived from the UrlRoutingHandler Class and add a new Route to the RouteCollection.

JamieSee
  • 12,696
  • 2
  • 31
  • 47
  • This is not an mvc app. Any guide/example pointing at creating custom route handlers? – xoail Jul 27 '12 at 21:35
  • I haven't tried it myself, but I think you may be able to just override the ProcessRequest method in your derived handler and have it do a `Response.Redirect` call. http://msdn.microsoft.com/en-us/library/cc680109(v=vs.90).aspx – JamieSee Jul 27 '12 at 23:15
1

In order to route to an extensionless target like /content/something you need to use Integrated Mode in the IIS application pool. If you cannot change this, then there's no easy way to do what you want, other than setting up a custom 404 handler, and manually dealing with the URL from there. This is probably the cause of your 404 errors.

Also, RouteExistingFiles should probably be false. You are trying to route things that don't exist as files. If you set RouteExistingFiles = true then the default handling for files that exist will NOT be used when a route matches. All you want to do is deal with paths that don't exist, but represent content from a database, right? So leave this false.

Finally, your MapPageRoute statement appears to be incorrect. For example, in Microsoft's walkthrough, it might look like: http://msdn.microsoft.com/en-us/library/dd329551.aspx

routes.MapPageRoute("SalesRoute",
    "SalesReport/{locale}/{year}",
    "~/sales.aspx");

That would cause matching routes to invoke sales.aspx in response. In your example, the target is ~/about/company which looks like a route, not an actual target - your actual target that handles the CMS request should have an aspx extension.

Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119
1

Hopefully this will help you, this specific example is for Sitecore but the project is open source and I imagine it would not be too difficult to re-purpose the parts you need for your solution.

The gist of this is project is to allow you to use the MVC routing engine with a webforms solution. I will add the cavet that you will need to dig into the code a bit but hopefully will put you on the right track.

https://github.com/Sitecore/Sitecore-Mvc-Routing

Steve Newstead
  • 1,223
  • 2
  • 10
  • 20