在 ASP.NET MVC 3 中路由静态文件,例如 robots.txt
我希望将以下链接“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以安装网址重写模块:
http://www.iis.net/downloads/microsoft/url-rewrite
请记住,此模块适用于 IIS 而不是 Cassini/IIS Express。
并将以下规则添加到您的 web.config 的
部分,我在新的 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>
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
您可以设置磁盘文件的路由请求。默认情况下,路由系统在评估应用程序的路由之前会检查 url 是否与磁盘文件匹配。如果存在匹配,则提供磁盘文件并且不使用路由。不过,这可以逆转,因此通过将
RouteCollection
的RouteExisitingFiles
属性设置为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 ofRouteCollection
totrue
. 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.解决方案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.
我可以通过重写 global.asax 中
BeginRequest
的事件处理程序中的路径来做到这一点。I was able to do this by rewriting paths in an event handler for
BeginRequest
in global.asax.添加这样的路线应该可以解决问题。这样,任何静态 .txt 文件(例如 robots.txt)都可以提供服务。
Adding a route like this should do the trick. This way any static .txt file like robots.txt can be served.