2

Hi all i have an aciton in my controller which generates an sitemap for my site, the problem here is for the first time when the file not exists the request goes to the action but once the file is generated , a static file is placed in the site , after that when i hit the URL the request is not going to the controller , is there a fix for this

Below is the URL

        http://localhost:1234/sitemap.xml

Below is the code am using

        [HttpGet, ActionName("sitemap")]
        public ActionResult sitemap()
        {           

           //will get the sitemap content here and will generate the sitemap 
            var sitemapFile = "~/sitemap.xml";
            return File(sitemapFile, "text/xml");
        }

Every time i hit the URL it should go to the action and regenerate the file , but this is not working here can any one help me out

2
  • Have you tried, localhost:1234/YourController/sitemap Commented May 27, 2015 at 12:40
  • @SHammelburg yes that works , but i want it to work this way , since we have to submit it to google bots with the sitemap.xml extension , thats the reason i have to write a custom route Commented May 27, 2015 at 12:42

2 Answers 2

2

When your sitemap.xml file exists IIS will pick up the request and serve your existing sitemap.xml. For more information on this matter, take a look at this article

If I understand correctly you want to route sitemap.xml requests to a MVC route, here is good blog post about that.

What you need to do first is to map the request of the sitemap.xml to the TransferRequestHandler. Add this to your web.config

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

Next configure the route in your RouteConfig.cs:

routes.MapRoute(
    name: "SiteMapRoute",
    url: "sitemap.xml",
    defaults: new { controller = "SEO", action = "Sitemap", page = UrlParameter.Optional }
);

And put this statement before you map any route

routes.RouteExistingFiles = true;

More information on RouteExistingFiles

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

2 Comments

Andrew , where do we need to the section .config file , and what is "SitemapFileHandler"
I guess you mean where to add the SiteMapFilHandler ? This needs to be in your web.config in the <system.webServer/handlers> node
1

Could you try this custom route please,

Add it to your RouteConfig.cs file

routes.MapRoute(
    name: "Sitemap",
    url: "Sitemap",
    defaults: new { controller = "Home", action = "Sitemap" }
);

You can access your Action like this,

http://localhost:1234/sitemap

Hope its what you're looking for.

4 Comments

this one will definitely work when i use the URL "localhost:1234/sitemap" , but i also want it to work when the URL is "localhost:1234/sitemap.xml"
If you have sitemap.xml in your root folder then it should work. I've just tried it and loads both ways.
Yes it loads , but the once the file exists, it never hits the action again untill the file is deleted
How are you creating the sitemap.xml?

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.