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

1
  • 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. Commented Feb 7, 2011 at 15:58

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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()
    )); 

2 Comments

no, it doesnt work. I get HTTP 404 when trying to get /sitemap.xml
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" })
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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.