I am working on converting a ASP.NET MVC application over to ASP.NET Core 9.0 MVC. It is going pretty well so far, but I am at the point where I need to allow my users to upload files and read files from a directory. In ASP.NET, I would create a virtual directory in IIS (/files/
) and all files that are dynamically created from the system or uploaded by users would be somewhere inside there. This virtual directory may point to one physical path on live, a different spot for staging, and another spot on any developer machines.
From what I am reading so far, it seems like virtual directories may not be supported in ASP.NET Core, but I don't really understand how to mimic that functionality. I need the path to be different based on which version of the application it is (live, staging, dev). I don't want it to be a folder that is a part of the project as I don't want any publish issues. What is the best way to proceed?
I am working on converting a ASP.NET MVC application over to ASP.NET Core 9.0 MVC. It is going pretty well so far, but I am at the point where I need to allow my users to upload files and read files from a directory. In ASP.NET, I would create a virtual directory in IIS (/files/
) and all files that are dynamically created from the system or uploaded by users would be somewhere inside there. This virtual directory may point to one physical path on live, a different spot for staging, and another spot on any developer machines.
From what I am reading so far, it seems like virtual directories may not be supported in ASP.NET Core, but I don't really understand how to mimic that functionality. I need the path to be different based on which version of the application it is (live, staging, dev). I don't want it to be a folder that is a part of the project as I don't want any publish issues. What is the best way to proceed?
Share Improve this question edited Mar 4 at 21:44 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 4 at 21:41 Mike SMike S 5491 gold badge5 silver badges10 bronze badges1 Answer
Reset to default 0You could host static files of a folder directly by:
var externalFilePath = builder.Configuration.GetValue<string>("ExternalFilePath");
var fileProvider = new PhysicalFileProvider(externalFilePath);
var requestPath = "/files";
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = fileProvider,
RequestPath = requestPath,
//ServeUnknownFileTypes = true // Only if needed
});
// for directory browsing
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = fileProvider,
RequestPath = requestPath
});
Then you add different path to json file of different enviroment. appsettings.Development.json
appsettings.staging.json
appsettings.prod.json
"ExternalFilePath": "C:\\ExternalFiles"
And you will need to create api endpoint to upload file to this folder
[ApiController]
[Route("api/files")]
public class FileUploadController : ControllerBase
{
private readonly string _externalPath;
public FileUploadController(IConfiguration configuration)
{
_externalPath = configuration.GetValue<string>("ExternalFilePath")
?? throw new Exception("External file path not configured");
}
[HttpPost("upload")]
public async Task<IActionResult> UploadFile(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("No file uploaded.");
string filePath = Path.Combine(_externalPath, file.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return Ok(new { FilePath = filePath });
}
}