0

I'm trying to add a route that will transfer all sitemap.xml requests to a custom request handler I made.

I tried using the following code:

        routes.Add(new Route("sitemap.xml", new Helpers.SiteMapRouteHandler()));
        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  
        );

But when I make a link using Url.Action():

Url.Action("Index", new { controller = "About"})

I get the following when I try to navigate to the XML file:

/sitemap.xml?action=Index&controller=About

What am I doing wrong?

ANSWER:

I used this solution:

Specifying exact path for my ASP.NET Http Handler

Community
  • 1
  • 1
Shay
  • 279
  • 2
  • 12
  • where do you have it in your list of routes? It appears pretty generic, so it would be matching on all requests. You may want to use Phil Haack's Route Debugger to help you out. – Jonathan Bates Feb 07 '11 at 15:58

3 Answers3

3

If you want to route to and action instead to a request handler

You could add a route like this

routes.MapRoute(
        "Sitemap",
        "sitemap.xml",
        new { controller = "Home", action = "SiteMap" }
        );

as mentioned here - MVC: How to route /sitemap.xml to an ActionResult? and it worked for me

Update: Also ensure that <modules runAllManagedModulesForAllRequests="true"> is set.

Community
  • 1
  • 1
Rajeesh
  • 4,377
  • 4
  • 26
  • 29
0

You have to add a handler to web.config file.

<add name="SitemapFileHandler" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>

And configure your routes:

routes.RouteExistingFiles = true;

Read more here: http://weblogs.asp.net/jongalloway//asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works

miyconst
  • 483
  • 4
  • 8
0

I'm not sure if this will solve the problem, but it's worth a try:

routes.Add(
    new Route(
        "{request}",
        new RouteValueDictionary(new { id = "" }),  // this might be the tricky part to change
        new RouteValueDictionary(new { request = "sitemap.xml" }),
        new Helpers.SiteMapRouteHandler()
    )); 
ARM
  • 2,385
  • 1
  • 13
  • 11
  • no, it doesnt work. I get HTTP 404 when trying to get /sitemap.xml – Shay Feb 07 '11 at 14:11
  • Wish I could have been more help. Only other thing I can think of is to try setting both RVD to (new { request = "sitemap.xml" }) – ARM Feb 07 '11 at 15:16