In some cases, the front end needs to download static files on the server to the local device. For most files, they can be downloaded directly using the HTTP Get method. However, for some special static files (such as .py files, .cs files, etc.), a 404 file not found error will be returned when we download them. The following uses the Kestrel server as an example to explain how to solve this problem.
Tip: Kestrel is a lightweight web server built into ASP.NET Core
First, enable static file browsing in the Program class:
app.UseStaticFiles();
Next, we need to add the MIME types that are allowed to be downloaded (using .py files as an example):
var sfo = new StaticFileOptions();
// Create a static file option and add the MIME type
var provider = new FileExtensionContentTypeProvider();
// Add support for the MIME type .py
provider.Mappings[‘.py’] = ‘text/x-python’;
sfo.ContentTypeProvider = provider;
Finally, pass the instance of StaticFileOptions to the UseStaticFiles method:
app.UseStaticFiles(sfo);
Why does this happen? The reason is simple: these special file extensions happen to be exactly the same as international top-level domains. For example, .py is the top-level domain of Paraguay, and .cs is the top-level domain of Czechoslovakia. After receiving your request, the web server will look for the IP address bound to this domain name, but it cannot be found, so it returns 404.
Top comments (0)