Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net core - How to serve static file with no extension

Tags:

ASP.NET Core hapily serves up files from the wwwroot folder based on the mime type of the file. But how do I get it serve up a file with no extension?

As an example, Apple require that you have an endpoint in your app /apple-app-site-association for some app-intergration. If you add a text file called apple-app-site-association into your wwwroot it won't work.

Some things I've tried:

1) Provide a mapping for when there's no extension:

var provider = new FileExtensionContentTypeProvider(); provider.Mappings[""] = "text/plain"; app.UseStaticFiles(new StaticFileOptions             {                 ContentTypeProvider = provider             }); 

2) Adding an app rewrite:

var options = new RewriteOptions() .AddRewrite("^apple-app-site-association","/apple-app-site-association.txt", false) 

Neither work, the only thing that does work is a .AddRedirect which I'd rather not use if possible.

like image 263
Matt Roberts Avatar asked Feb 13 '18 14:02

Matt Roberts


People also ask

How do I serve a static file in NET Core?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

Where do I put static files in NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method.

Which of the following static files can ASP.NET Core application serve?

ASP.NET Core can serve static files—HTML files, images, JavaScript files, etc. —directly to clients. To enable ASP.NET Core to serve static files, you must use the framework's Static Files Middleware in your application, and you must specify the necessary configuration.

What is app UseStaticFiles () in .NET Core?

UseStaticFiles() method adds StaticFiles middleware into the request pipeline. The UseStaticFiles is an extension method included in the StaticFiles middleware so that we can easily configure it. Now, open the browser and send http request http://localhost:<port>/default.html which will display default.


1 Answers

Adding an alternative solution. You have to set ServeUnknownFileTypes to true and after that set the default content type.

        app.UseStaticFiles(new StaticFileOptions         {             ServeUnknownFileTypes = true,             DefaultContentType = "text/plain"         }); 
like image 183
sinofis Avatar answered Oct 05 '22 17:10

sinofis