简体   繁体   English

从另一个项目访问文件与ASP.NET MVC相同的解决方案

[英]Access files from another project same solution asp.net mvc

I have solution "solution" and two projects: 我有解决方案“解决方案”和两个项目:

  • solution.WebUI (here user uploads file to some folder like "~/uploads" solution.WebUI (此处用户将文件上传到“〜/ uploads”之类的文件夹中
  • solution.WebApi (here I must access user files) solution.WebApi (在这里我必须访问用户文件)

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.

相关问题 Asp.Net Core v1.1从同一解决方案中的另一个项目获取继承的类名 - Asp.Net Core v1.1 Get Inherited Class Names From another project in same solution 如何使用 Src asp.net 内核从同一解决方案中的另一个项目获取图像? - how to get image from another project in the same solution with Src asp.net core? ASP.NET Core:从解决方案中的另一个项目访问appsettings - ASP.NET Core: Accessing appsettings from another project in solution 从IIS上存储的文件中恢复ASP.NET MVC项目 - Recover ASP.NET MVC Project From Files Stored On IIS ASP.NET MVC 4项目的DBML文件 - DBML files for ASP.NET MVC 4 project 如何在同一项目中的ASP.NET MVC控制器中从ASP.NET Web API获取数据 - How get data from asp.net web api in asp.net mvc controller in the same project 如何在Singe Solution ASP.net中将控制权从一个项目转移到另一个项目? - How to transfer control from one Project to another project in a Singe Solution ASP.net? asp.net mvc如何将业务逻辑从另一个项目绑定到我的asp.net mvc项目 - asp.net mvc how to bind business logic from another project to my asp.net mvc project SonarQube Runner在Asp.Net MVC解决方案中跳过JavaScript个文件 - SonarQube Runner skips JavaScript files in Asp.Net MVC solution 是否可以在另一个解决方案中从 ASP.NET Webforms 项目中引用 ASP.NET Core 2.0 类库 dll? - Possible to reference an ASP.NET Core 2.0 class library dll from an ASP.NET Webforms project in another solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM