2

so here's what my RouteConfig looks like:

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute("Robots.txt",
            "robots.txt",
            new { 
                controller = "Home", 
                action = "Robots", 
                id = "", 
                language = "en", 
                culture = "us" 
            });
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default",
            "{language}/{culture}/{controller}/{action}/{id}",
            new
            {
                controller = "Home",
                action = "Index",
                id = "",
                language = "en",
                culture = "us"
            });
    }
}

Now according to my understanding, The site should route to robots.txt when I go to www.mysite.com/robots.txt, however it is not. I also had problems serving another static page, but I found a workaround and didn't have to serve the static .html page.

I have built another site in MVC3 and I've copied the route config patterns I used there and they seem to work fine. Any ideas why this would not work?

P.S. I see the robots.txt file just fine when I browse to www.mysite.com/en/US/Home/Robots so all the controllers, actions, & views are matching up just fine.

Thanks!

4

1 回答 1

6

That might be because IIS thinks it is a static file because of the extension and not passing it to the managed pipeline for processing.

You could add the following handler to the <system.webServer> section of your web.config file in order to make all requests to /robots.txt go through the managed pipeline (and thus through the routes you might have setup):

<system.webServer>
    <handlers>
        ...
        <add name="Robots" path="robots.txt" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

You might also make this work by enabling managed modules for all requests but I wouldn't recommend you doing that as all requests will now go through the managed pipeline which might have an additional overhead:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    ...
</system.webServer>
于 2013-03-30T22:56:09.317 回答