0

I've seen this code a ton on this site, but I can't seem to get it to work. When I navigate to www.mysite.com/sitemap.xml it gives me a 404 error. Here is my code for the RouteTable setup:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Clear();
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.RouteExistingFiles = true;

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Here is the Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Would someone please point out what I've done wrong? It looks like the examples given in questions such as C# mvc3 redirect sitemap.xml to controller action.

Community
  • 1
  • 1
Middas
  • 1,862
  • 1
  • 29
  • 44
  • 1
    Put it in debug and check where it's actually trying to route to.. – Simon Whitehead Dec 05 '12 at 05:36
  • I didn't know you could debug the routing. Where would I put the break point? – Middas Dec 05 '12 at 05:38
  • I mean the website.. is it showing a 404 error or the .NET 'Cannot find view/route' error page? – Simon Whitehead Dec 05 '12 at 06:06
  • Detailed Error Information: Module IIS Web Core Notification MapRequestHandler Handler StaticFile Error Code 0x80070002 Requested URL http://localhost:8295/sitemap.xml Physical Path C:\Users\Dev\Documents\Dev\Site\sitemap.xml Logon Method Anonymous Logon User Anonymous Request Tracing Directory C:\Users\Dev\Documents\IISExpress\TraceLogFiles\SITE Is this the information you needed? – Middas Dec 05 '12 at 06:13
  • It's definitely a 404 error. "HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." – Middas Dec 05 '12 at 06:21
  • I noticed the handler is listed as StaticFile, is that the issue? – Middas Dec 05 '12 at 06:22
  • In a test, I removed the StaticFile handler, now it just says "Not yet determined" for the handler. – Middas Dec 05 '12 at 06:31

2 Answers2

2

After more trial and error playing with handlers, I was able to get it working. It turns out that StaticFile handler was indeed the issue. I was able to fix it by adding the following to my web.config file:

<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit_XML" path="*.xml" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit_XML" path="*.xml" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0_XML" path="*.xml" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Middas
  • 1,862
  • 1
  • 29
  • 44
2

You can also use this shorter version found in this answer

<handlers>
<add name="xml-file-handler" path="*.xml" type="System.Web.UI.PageHandlerFactory" verb="*" />
</handlers>
Community
  • 1
  • 1
David Smit
  • 829
  • 1
  • 13
  • 31