![](/img/trans.png)
[英]ASP.NET HttpHandler mapping to Robots.txt without access to IIS
[英]ASP.NET Core not serving robots.txt
在我的 Startup 類的Configure
方法中,我為單獨構建的單頁應用程序進行了以下設置:
app.UseStaticFiles();
app.Run(async (context) =>
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "index.html"));
});
然后,在應用程序的 wwwroot 中,我有一個 robots.txt 文件(與我的 JS/CSS 文件相同級別)。 JS/CSS 文件由靜態文件中間件提供良好的服務,但對 robots.txt 的請求總是落入包羅萬象的中間件。
如何讓它也將 robots.txt 作為靜態文件提供服務?
您需要將這些行添加到您的 Configure 方法中:
using AspNetCore.SEOHelper;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// add these two lines to those you already have
app.UseRobotsTxt(env.ContentRootPath);
app.UseStaticFiles();
}
您將需要安裝 AspNetCore.SEOHelper 包
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.