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

后端 未结 3 1981
执念已碎
执念已碎 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: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 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.

提交回复
热议问题