have a problem accessing the files in wwwroot - asp.net-core

In the wwwroot folder I can access all files(images, java script, css, zip). But when I upload an apk file, it is not accessible.
When I compress this apk file to zip I can download it

AspNetCore uses this list of media types and according to this list, it does not know what an APK file is, so AspNetCore will return a 404 error.
To allow this, you can map your own. REF: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0#fileextensioncontenttypeprovider
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".apk"] = "application/vnd.android.package-archive";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});

Related

The fbx file can't load in ASP.NET core

I use three.js in my project. In developing front-end everything was OK, but after copy/past code in .cshtml files and migrate static files in wwwroot, a JavaScript file contatins threejs loader cannot load .fbx file. After run project give 404 error.
This code get .fbx file.
let loader = new FBXLoader();
loader.load("/fbx/earthfbx2.fbx", function (object) {
//do somethings
});
after some search, I improve problem by adding this lines of code in startup.cs Configure method
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
provider.Mappings[".fbx"] = "application/octet-stream";
provider.Mappings[".myapp"] = "application/x-msdownload";
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
ContentTypeProvider = provider
});

Using virtual directory with ASP.NET Core 3.1

In ASP.NET framework 4.8 and below version the web application use IIS for processing and show data. And when we need to work with other resource like folders and network resource we can easily create Virtual Directory in IIS and map to physical path and my application work theme like normal folder.
but in Net Core we use Kestrel and IIS work as a proxy and Virtual Directory not recognized by Kestrel. I use this code but not work in function. Write this section in Configure Function
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(#"E:\\MyFiles"),
RequestPath = new PathString("/PFiles"),
EnableDirectoryBrowsing = true
});
and this is my code :
var folder = Path.Combine(environment.WebRootPath, "temp");
var fileName = Path.Combine(environment.WebRootPath, "temp\\test.jpeg");
var basePath = Path.Combine(environment.WebRootPath, "PFiles");
var yearPath = Path.Combine(basePath, "2020");
var monthpath = Path.Combine(basePath, "2020", "06");
if (!Directory.Exists(yearPath))
{
Directory.CreateDirectory(yearPath);
}
if (!Directory.Exists(monthpath))
{
Directory.CreateDirectory(monthpath);
}
var dpath = Path.Combine(basePath, "2020", "06");
var destinationPath = Path.Combine(environment.WebRootPath, dpath);
System.IO.File.Move(fileName, destinationPath);
but I receive Access denied Error or create directory inside of wwwroot folder. So, what must I do?
you get this error because iis default account does not have enough permission to access the folder.
you could try to assign the iis_iusrs , iusr, or IIS AppPool<myappoolname> permission to the folder where you want to create directory.

ASP.NET Core can't see images when acceding by browser

I've created an ASP.NET Core 3.1 Web API project, on IIS Express it works perfectly with the controllers. I've created a folder called images and copied some .jpg files there.
My idea was get those images from my Xamarin app using the url, like
http://192.168.0.185:52493/img/lucas.PNG
but nothing is being returned. Then I've tried writing the url directly into Internet Explorer, but nothing happens, the image is not shown in the browser either.
Any idea what it can be?
If you would like to access your file from your own folder img located in the root of the project instead of the default wwwroot folder ,you need to configure the Static File Middleware in startup.cs Configure method like below:
app.UseStaticFiles();// For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "img")),
RequestPath = "/img"
});
app.UseRouting();
//other middlewares
Refer to https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-files-outside-of-web-root

Upload Folders to Fileshare with ASP.NET Core Website

I want my Uploads folder to reside on a fileshare.
Reason why I want this: Redundant frontend.
So instead of saving to:
C:\inetpub\wwwroot\wwwroot\Uploads
Uploads should be saved to:
\\fileshare01\MyWebsite\Uploads
I am aware that VirtualDirectories exist. This works for reading from the fileshare but writing to the Uploads directory still writes to lokal drive.
So with VirtualDirectories I can access http://localhost/Uploads/myfile.png which is actually on the fileshare BUT new files are not written there!
Here (simplified) how I save files:
IFormFileCollection files = Request.Form.Files;
var file = files.First();
using (var stream = new FileStream(#"Uploads\myfile.png", FileMode.Create))
{
await file.CopyToAsync(stream);
}
When I try to save to the new network path as absolute path it seems I require higher permissions and end up with a 500.30 error. I guess because application pool user has too little permission which I think is a good thing.
My Question:
How is this problem solved as good-practice? Shouldn't everything work automagically when configuring a VirtualDirectory including writing?
Solved it. I just got the 500.30 error because of an error in my appconfig.json. I didn't escape the backslashes in my base path.
I found this blog post saying
There is no need to add a „virtual directory“ in IIS, this stuff is
deprecated
and explaining that this is the way it's done via Startup.cs Configure() method:
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(#"\\server\path"),
RequestPath = new PathString("/MyPath"),
EnableDirectoryBrowsing = false
});
Another configuration mystery of ASP.NET Core solved :)

How can you use multiple directories for static files in an aspnet core app?

By default, the wwwroot directory can host static files. However, in some scenarios, it might be ideal to have two directories for static files. (For example, having webpack dump a build into one gitignored directory, and keeping some image files, favicon and such in a not-gitignored directory). Technically, this could be achieved by having two folders within the wwwroot, but it might organizationally preferable to have these folders at the root level. Is there a way to configure an aspnet core app to use two separate directories for static files?
Just register UseStaticFiles twice:
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "static"))
});
Now files will be found from wwwroot and static folders.
Registering UseStaticFiles twice won't solve it for MapFallbackToFile
An alternative approach.
// Build the different providers you need
var webRootProvider =
new PhysicalFileProvider(builder.Environment.WebRootPath);
var newPathProvider =
new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, #"Other"));
// Create the Composite Provider
var compositeProvider =
new CompositeFileProvider(webRootProvider, newPathProvider);
// Replace the default provider with the new one
app.Environment.WebRootFileProvider = compositeProvider;
https://wildermuth.com/2022/04/25/multiple-directories-for-static-files-in-aspnetcore/