Server.MapPath is not available in asp.net core. Instead, you can use IWebHostEnvironment interface to access any physical contents kept in the specified path.
Below is the Controller code using the IWebHostEnvironment ContentRootPath property which gets the absolute path to the directory that contains the Application content files:
[ApiController]
public class DocumentController : ControllerBase
{
private IDocumentService offService = null;
private readonly IWebHostEnvironment _host;
public DocumentController(IDocumentService offService, IWebHostEnvironment host)
{
this.offService = offService;
this._host = host;
}
[HttpGet]
[Route("some/route")]
public IActionResult ExportToFile([FromQuery] int UserId)
{
var username = HttpContext.User.Identity.Name.Split('\\')[1].ToString();
//get the memoryStream by passing the absolute path:
MemoryStream memoryStream = offService.GetWordStream(_host.ContentRootPath);
memoryStream.Position = 0;
return File(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "File.docx"); // returns a FileStreamResult
}
}
Use the path in the File IO methods, example below:
File.Copy(path + "\\DocPath\\File.docm", strFileName);
In the debug mode, this path is the WebAPI Project absolute path to the directory. During runtime, this path is the absolute path where the files are hosted on the Server.