简体   繁体   English

ASP.NET Core 不提供 robots.txt

[英]ASP.NET Core not serving robots.txt

In my Startup class' Configure method I have the following setup for a single-page application that's built separately:在我的 Startup 类的Configure方法中,我为单独构建的单页应用程序进行了以下设置:

app.UseStaticFiles();

app.Run(async (context) =>
{
    context.Response.ContentType = "text/html";
    await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "index.html"));
});

Then, in the wwwroot of the app I have a robots.txt file (same level as my JS/CSS files).然后,在应用程序的 wwwroot 中,我有一个 robots.txt 文件(与我的 JS/CSS 文件相同级别)。 The JS/CSS files are served fine by the static files middleware, but requests to robots.txt always falls through to the catch-all middleware. JS/CSS 文件由静态文件中间件提供良好的服务,但对 robots.txt 的请求总是落入包罗万象的中间件。

How can I make it serve robots.txt as a static file too?如何让它也将 robots.txt 作为静态文件提供服务?

You need to add these lines to your Configure method:您需要将这些行添加到您的 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();
}

You will need to install the AspNetCore.SEOHelper package您将需要安装 AspNetCore.SEOHelper 包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM