[英]Access files from another project same solution asp.net mvc
I have solution "solution" and two projects: 我有解决方案“解决方案”和两个项目:
In web api project I access files like this: 在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;
}
How must I modify path to file? 如何修改文件路径?
我认为共享文件夹是一个更好的解决方案http://support.microsoft.com/kb/324267
I also faced a similar kind of problem where I need to access the Upload folder of another project(main) from the BLL project under same solution. 我还遇到了类似的问题,需要以相同的解决方案从BLL项目访问另一个项目(主)的Upload文件夹。 For that I have used absolute path(not hardcoded) for the Upload folder.
为此,我对上载文件夹使用了绝对路径(未硬编码)。 I think the code below should also work for you.
我认为以下代码也应为您工作。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.