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

后端 未结 3 1979
执念已碎
执念已碎 2020-12-06 10:05

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?

A

相关标签:
3条回答
  • 2020-12-06 10:34

    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"
            });
    
    0 讨论(0)
  • 2020-12-06 10:36

    An easier option may be to put a file with a proper extension on the server, and then use URL rewrite as follows.

    app.UseRewriter(new RewriteOptions()
        .AddRewrite("(.*)/apple-app-site-association", "$1/apple-app-site-association.json", true));
    
    0 讨论(0)
  • 2020-12-06 10:47

    Rather than fighting with static files, I think you'd be better off just creating a controller for it:

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using System.IO;
    
    namespace MyApp.Controllers {
        [Route("apple-app-site-association")]
        public class AppleController : Controller {
            private IHostingEnvironment _hostingEnvironment;
    
            public AppleController(IHostingEnvironment environment) {
                _hostingEnvironment = environment;
            }
    
            [HttpGet]
            public async Task<IActionResult> Index() {
                return Content(
                    await File.ReadAllTextAsync(Path.Combine(_hostingEnvironment.WebRootPath, "apple-app-site-association")),
                    "text/plain"
                );
            }
        }
    }
    

    This assumes your apple-app-site-association file is in your wwwroot folder.

    0 讨论(0)
提交回复
热议问题