question

AbdelrahmanTarek-8600 avatar image
0 Votes"
AbdelrahmanTarek-8600 asked AbdelrahmanTarek-8600 commented

.Net 6 IWebHostENvironment returns local path that exists on server

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://MyDomain//Folder//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

sql-server-generaldotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ZhiLv-MSFT avatar image
1 Vote"
ZhiLv-MSFT answered AbdelrahmanTarek-8600 commented

Hi @AbdelrahmanTarek-8600,

You should build the file path by yourself. Refer to the following sample:

         var foldername = "images";
         var filename = "Image1.png";
         var path1 = Path.Combine(_environment.WebRootPath, foldername, filename);
         var path2 = Path.Combine("\\", foldername, filename); 
         var location = new Uri($"{Request.Scheme}://{Request.Host}/{foldername}/{filename}"); 
         var url = location.AbsoluteUri; 

If you want to upload/download file from the wwwroot folder, you can get the file path via the IWebHostENvironment.

If you want to display the image using image control, you can use the relative paths, such as path2 in the above sample.

If you want to store the file path using the request host, directly build the request url, like the path3:

The result as below:

262369-image.png


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Dillion


image.png (477.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm trying to build this URI inside the application layer, i don't write my function inside the controller body, i build the command in the application layer and just pass an instance from the controller, so do you know a Microsoft extension or interface like the iWebHost for example so i can access the "Request.Scheme", "Request.Host", inside that layer?

0 Votes 0 ·

Thank you I found the IHttpContextAccessor and i can access the request outside of the Controller

0 Votes 0 ·