如何在asp.net mvc 3中获取FilePathResult文件的完整文件路径?

问题描述:

我想获取操作方法返回的文件的最后修改日期。我想我需要一个完整的文件路径。 FilePathResult有财产FileName如何在asp.net mvc 3中获取FilePathResult文件的完整文件路径?

此属性是否返回完整的文件路径或只是一个名称?如果是这样,我可以以某种方式获得该文件的完整路径吗?

谢谢!

它返回文件的完整路径。例如:

[MyActionFilter] 
public ActionResult Index() 
{ 
    return File(Server.MapPath("~/web.config"), "text/xml"); 
} 

然后:

public class MyActionFilterAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     var fileResult = filterContext.Result as FilePathResult; 
     if (fileResult != null) 
     { 
      // here you will get the full absolute path to the file, 
      // for example c:\inetpub\wwwroot\MvcApplication1\web.config 
      string fileName = fileResult.FileName; 
     } 
    } 
} 
+0

感谢答案。我指望它返回完整的文件路径。我想设置文件的最后修改日期(首先我需要得到它)。出于某种原因,我无法正确设置(或获取)修改日期。你能检查我的更新吗?提前致谢! – 2013-02-17 17:54:01

+0

@AleseseiChepovoi,这是一个完全不同的questoin与你原来的问题没有任何关系,并且是这个副本:http://stackoverflow.com/questions/14914228/client-side-caching-using-last-modified- header-and-outputcacheattribute -in-asp-n请不要发表重复的问题。在这里,您最初询问'FileName'属性是否返回文件的完整路径,并且答案是肯定的。如果你需要讨论一些其他的代码,让它发生在适当的地方。 – 2013-02-17 17:56:21