从另一个项目访问文件相同的解决方案asp.net mvc

时间:2013-03-18 16:41:24

标签: c# asp.net-mvc-4 asp.net-web-api filepath

我有解决方案“解决方案”和两个项目:

  • solution.WebUI (此处用户将文件上传到某个文件夹,如“〜/ uploads”
  • solution.WebApi (这里我必须访问用户文件)

在web api项目中,我访问这样的文件:

    public HttpResponseMessage GetPdfPage()
    {
        HttpResponseMessage responce =  new HttpResponseMessage();
        responce.Content = new StreamContent(new FileStream(HttpContext.Current.Server.MapPath("~/somefile.pdf"), FileMode.Open, FileAccess.Read));
        responce.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

        return responce;
    }

如何修改文件路径?

2 个答案:

答案 0 :(得分:1)

我认为共享文件夹是更好的解决方案 http://support.microsoft.com/kb/324267

答案 1 :(得分:0)

我还遇到了类似的问题,我需要在同一解决方案下从BLL项目访问另一个项目(主要)的Upload文件夹。 为此,我使用绝对路径(非硬编码)为Upload文件夹。我认为下面的代码也适合你。

public HttpResponseMessage GetPdfPage()
    {
        HttpResponseMessage responce =  new HttpResponseMessage();
        string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
        responce.Content = new StreamContent(new FileStream(HttpContext.Current.Server.MapPath(basePath +"somefile.pdf"), FileMode.Open, FileAccess.Read));
        responce.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

        return responce;
    }