在 ASP.NET MVC 3 中路由静态文件,例如 robots.txt

发布于 2024-12-18 06:18:27 字数 107 浏览 3 评论 0原文

我希望将以下链接“http://mywebsite.com/robots.txt”链接到静态文件 ~/Content/robots.txt。

我该怎么做?

谢谢, 梅林

I would like to have the following link "http://mywebsite.com/robots.txt" linked to a static file ~/Content/robots.txt.

How can I do this?

Thanks,
Merijn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤独陪着我 2024-12-25 06:18:28

您可以安装网址重写模块
http://www.iis.net/downloads/microsoft/url-rewrite
请记住,此模块适用于 IIS 而不是 Cassini/IIS Express。

并将以下规则添加到您的 web.config 部分,

<rewrite>
    <rules>
        <rule name="robots" stopProcessing="true">
            <match url="robots.txt" />
            <action type="Rewrite" url="/Content/robots.txt" />
        </rule>
    </rules>
</rewrite>

我在新的 MVC 3 .NET 项目上检查了它,并且两个 URL 响应都使用同一文件:
mywebsite.com/robots.txt
mywebsite.com/Content/robots.txt

You could install Url Rewrite module:
http://www.iis.net/downloads/microsoft/url-rewrite
Remember that this module works on IIS and not on Cassini/IIS Express.

And add following rule to your web.config to section <system.webServer>

<rewrite>
    <rules>
        <rule name="robots" stopProcessing="true">
            <match url="robots.txt" />
            <action type="Rewrite" url="/Content/robots.txt" />
        </rule>
    </rules>
</rewrite>

I checked it on new MVC 3 .NET project and both url responses with the same file:
mywebsite.com/robots.txt
mywebsite.com/Content/robots.txt

苏大泽ㄣ 2024-12-25 06:18:28

您可以设置磁盘文件的路由请求。默认情况下,路由系统在评估应用程序的路由之前会检查 url 是否与磁盘文件匹配。如果存在匹配,则提供磁盘文件并且不使用路由。不过,这可以逆转,因此通过将 RouteCollectionRouteExisitingFiles 属性设置为 true,在检查磁盘文件之前先查看路由。将此语句放在靠近 RegisterRoutes 方法顶部的位置 - 这似乎是 mvc 应用程序的惯例。然后定义磁盘文件的路由。请注意,执行此操作时可能会产生一些不可预见的影响,因为仪式可能会捕获其他类型的 URL。

You can setup routing request for disk files. By default the routing system checks to see if the url matches the disk file before evaluating the application's routes. If there is a match the disk file is served and routes are not used. However this can be reveresed so routes are looked at before disk files are checked by setting the RouteExisitingFiles property of RouteCollection to true. Place this statement close to the top of the RegisterRoutes method - this just seems to be convention for mvc apps. Then you define a route that for the disk files. Be aware when doing this that there can some unforseen effects because the riute could natch other kinds of URLs.

揪着可爱 2024-12-25 06:18:28

解决方案1:如果指定URL。浏览器将向 IIS 或网络服务器请求此信息。 MVC不参与读取文件等,它把这些请求交给IIS来处理。您需要为该文件夹分配权限。

解决方案 2:读取PresentationModel 中的文件(如果有)。以块的形式读取此文件并以文件类型返回到浏览器。

我希望它能给你一些指导。

Solution 1: If you specify the URL. Browser will request this to IIS or webserver. MVC doesn't participate in the reading file etc. It gives these requests to IIS to handle. You need to assign permission to the folder.

Solution 2: Read the file in the PresentationModel if you have. read this file in Chunks and return as File type to the browser.

I hope it will give you some direction.

烟雨扶苏 2024-12-25 06:18:28

我可以通过重写 global.asax 中 BeginRequest 的事件处理程序中的路径来做到这一点。

BeginRequest += delegate
{
    switch (Request.RawUrl.ToLowerInvariant())
    {
        case "/favicon.ico":
            Context.RewritePath("~/Content/favicon.ico");
            return;
        case "/robots.txt":
            Context.RewritePath("~/Content/robots.txt");
            return;
    }
};

I was able to do this by rewriting paths in an event handler for BeginRequest in global.asax.

BeginRequest += delegate
{
    switch (Request.RawUrl.ToLowerInvariant())
    {
        case "/favicon.ico":
            Context.RewritePath("~/Content/favicon.ico");
            return;
        case "/robots.txt":
            Context.RewritePath("~/Content/robots.txt");
            return;
    }
};
兔小萌 2024-12-25 06:18:28
 routes.MapRoute("Robots","robots.txt");
 routes.MapRoute("Robots","robots.txt");
岁月打碎记忆 2024-12-25 06:18:27

添加这样的路线应该可以解决问题。这样,任何静态 .txt 文件(例如 robots.txt)都可以提供服务。

routes.IgnoreRoute("{resource}.txt"); 

Adding a route like this should do the trick. This way any static .txt file like robots.txt can be served.

routes.IgnoreRoute("{resource}.txt"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文