Reputation: 575
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
Upvotes: 14
Views: 4925
Reputation: 877
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
Upvotes: 7
Reputation: 23483
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;
}
};
Upvotes: 0
Reputation: 31353
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");
Upvotes: 11
Reputation: 2120
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.
Upvotes: 5
Reputation: 24
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.
Upvotes: 0