asp.net core - 如何提供没有扩展名的静态文件 [英] asp.net core - How to serve static file with no extension
问题描述
ASP.NET Core 根据文件的 MIME 类型愉快地提供 wwwroot
文件夹中的文件.但是我如何让它提供一个没有扩展名的文件?
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?
例如,Apple 要求您的应用程序 /apple-app-site-association
中有一个端点用于某些应用程序集成.如果您将一个名为 apple-app-site-association 的文本文件添加到您的 wwwroot 中,它将不起作用.
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.
我尝试过的一些事情:
1) 提供没有扩展时的映射:
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) 添加应用重写:
var options = new RewriteOptions()
.AddRewrite("^apple-app-site-association","/apple-app-site-association.txt", false)
两者都不起作用,唯一起作用的是 .AddRedirect
,如果可能,我宁愿不使用它.
Neither work, the only thing that does work is a .AddRedirect
which I'd rather not use if possible.
推荐答案
与其与静态文件作斗争,我认为您最好为其创建一个控制器:
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"
);
}
}
}
这假设您的 apple-app-site-association
文件在您的 wwwroot 文件夹中.
This assumes your apple-app-site-association
file is in your wwwroot folder.
这篇关于asp.net core - 如何提供没有扩展名的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!