asp.net核心-如何提供无扩展名的静态文件 [英] 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
中具有终结点才能进行某些应用程序集成.如果您在wwwroot中添加一个名为apple-app-site-association的文本文件,将无法使用.
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)添加应用程序重写:
2) Adding an app rewrite:
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核心-如何提供无扩展名的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!