I’ve encountered with some strange problem. It’s strange for me because I don’t understand it and because before everything worked fine. So, my task is to call for controller and pass it file name (with extension) and controller should recognize this file, write into log and then return the file itself if it exists (in Downloads folder). What I’m doing:
public class DownloadController : Controller
{
public ActionResult Files(string id)
{
string filePath = Server.MapPath(Url.Content(string.Format("~/Downloads/{0}", id)));
string serverPath = Url.Content(string.Format("~/Downloads/{0}", id));
string ext = Path.GetExtension(filePath);
if (!System.IO.File.Exists(filePath))
{
//return to the error controller
}
string mem = "text/html";
if (ext == ".zip")
{
mem = "application/x-zip-compressed";
}
else if (ext == ".html" || ext == ".htm")
{
mem = "text/html";
}
else if (ext == ".pdf")
{
mem = "application/pdf";
}
//Save info about downloads into DB
repStat.SaveStatInfo(id, HttpContext.Request.UserHostAddress,
HttpContext.Request.UserHostName, HttpContext.Request.UserAgent);
return File(serverPath, mem, id);
}
}
There is part of the Global.asax:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("Content/{*pathInfo}");
It doesn’t have more “ignores”.
So, the problem is when I’m doing the call: mysite.com/download/files/test.pdf the server returns me “Page not found”. Of course there is no such file in download/files path! It should call for the controller but not the real file. As soon as I delete the extension like mysite.com/download/files/test the server calls for the controller. I don’t understand why it doesn’t recognize the file name just as parameter and tries to find the file.
There is absolutely the same behaviour if I try to do with other controllers – as soon as parameter doesn’t have any extension it works good else the server looks for the file.
The most strange thing is everything works good locally but doesn’t work on the server (worked not tool long but then stopped).
It looks strange to me too. A workaround would be to encode the dot (.) like, for example,
and then decode it then decode it back.
Probably not the best solution, but will certainly solve the problem.