Home > database >  .Net 6 IWebHostENvironment returns local path that exists on server
.Net 6 IWebHostENvironment returns local path that exists on server

Time:11-23

I have a .net core web api deployed on smarter ASP.net services, in my web api I save images and files by adding them to a predefined folder in wwwroot file that exists in the presistance project, and then i save the url in SQL database. This is my code to save the file and get the url using IWebHostEnvironment, after sending the request the saved url is like this "h:\root\home\siteurl\OriginalForms\MySavedFile", This is the path for the file on the server itself, i want to save the url itself like "http://domain//wwwroot//myfoldrr/file" not the local one on the server.

MyCode :

var path = Path.Combine(_webHostEnvironment.WebRootPath "OriginalForms",request.FormName); using (FileStream stream = new FileStream(path, FileMode.Create))
{
   await request.Form.CopyToAsync(stream,cancellationToken);
}
OriginalForm.Url = path;

CodePudding user response:

You could generate the uri of the pic ture as below:

var scheme = HttpContext.Request.Scheme;
var host=HttpContext.Request.Host.Value;
var floderpath = "wwwroot/";
var filename = "favicon.ico";
var loaction = new Uri($"{scheme}://{host}/{floderpath}{filename}");

And the Uri should contain wwwroot by default

http://domain//wwwroot//myfoldrr/file

If you do want to keep the uri,you have to set as the enter image description here

  • Related