ASP.NET Core can't see images when acceding by browser - asp.net-core

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

Related

have a problem accessing the files in wwwroot

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
});

Do I need to put css/js all in wwwroort folder in Asp.net Core? I am migrating from Older Mvc project

I want to use js/files outside wwwroot folder. How to Do that?
Yes, you can serve static files outside wwwroot.
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
RequestPath = "/StaticFiles"
});
}
See Serve files outside of web root.
But why do you want this?

.Netcore 2.0 and Virtual Directory

I have a .netcore 2.0 project running on IIS windows 2012.
There is an issue as I can't seem to get virtual directories working that exist outside of the root of the application. This works fine when the files are in the solution:
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), #"wwwroot", "images")),
RequestPath = new PathString("/MyImages")
});
The below does not however when outside of the root folder -
app.UseDirectoryBrowser(new DirectoryBrowserOptions()
{
FileProvider = new PhysicalFileProvider(Configuration["Keys:UploadFolder"]),
RequestPath = new PathString("/upload")
});
The reason for doing this is I have setup CI with octopus deploy. Each time I do a deployment the uploaded files are no longer there without using a virtual directory or creating some powershell script in octopus to copy them all over from last deployment.
Any recommendations or ways of resolving this in IIS with virtual directories in .netcore 2.0?

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/

Configure ASP.NET Core App to set new default files

I went thru the documentation provided for Use Static Files for ASP.NET core app. After Reading information from section Serving a default document & Using the UseFileServer method, following two questions are opened in my mind, respectively:
How can I add new default file if it is outside of wwwroot
How can I add new default file which is even under www using UseFileServer extension method
In your Startup.Configure method you can configure the default file:
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("myDefault.html"); // this had no influence :-(
app.UseDefaultFiles(options);
If I define the following options, I can load the default index.html through /StaticFiles but not my customized myDefault.html which is probably what you are after.
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(#"C:\temp\"),
RequestPath = new PathString("/StaticFiles"),
EnableDefaultFiles = true,
EnableDirectoryBrowsing = false
});
UseDefaultFiles doesn't seem to have any influence. However it should still work if you configure the default file in your web server.
FileServerOptions has a property DefaultFiles, but it is read-only.