Robots.AspNet
1.2.0
dotnet add package Robots.AspNet --version 1.2.0
NuGet\Install-Package Robots.AspNet -Version 1.2.0
<PackageReference Include="Robots.AspNet" Version="1.2.0" />
<PackageVersion Include="Robots.AspNet" Version="1.2.0" />
<PackageReference Include="Robots.AspNet" />
paket add Robots.AspNet --version 1.2.0
#r "nuget: Robots.AspNet, 1.2.0"
#:package Robots.AspNet@1.2.0
#addin nuget:?package=Robots.AspNet&version=1.2.0
#tool nuget:?package=Robots.AspNet&version=1.2.0
Robots.AspNet
A lightweight ASP.NET Core library that serves a dynamic robots.txt file via middleware, sets X-Robots-Tag headers on matching paths, with a fluent builder API and appsettings.json support.
Quick start
Install:
dotnet add package Robots.AspNet
In Program.cs:
using Robots.AspNet;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRobotsTxt()
.Disallow("GPTBot", "/")
.Disallow("*", "/admin/", "/login/")
.Allow("/login/blabla")
.CrawlDelay(10)
.Sitemap("https://example.com/sitemap.xml")
.AddRobotsTag("/admin/*", "noindex, nofollow")
.Comment("Generated by Robots.AspNet");
var app = builder.Build();
app.UseRobotsTxt();
app.Run();
API
AddRobotsTxt() — returns IRobotsTxtBuilder
services.AddRobotsTxt()
services.AddRobotsTxt(IConfiguration)
services.AddRobotsTxt(Action<RobotsTxtOptions>)
IRobotsTxtBuilder fluent methods:
| Method | Description |
|---|---|
Disallow(ua, paths...) |
Blocks the given paths for a user-agent |
Allow(path) |
Allows a path under the last user-agent |
Allow(ua, path) |
Allows a path for a specific user-agent |
Sitemap(url) |
Adds a Sitemap: record |
CrawlDelay(seconds) |
Sets Crawl-delay: under the last user-agent |
CrawlDelay(ua, seconds) |
Sets Crawl-delay: for a specific user-agent |
Comment(text) |
Adds a # comment at the top of the file |
Host(url) |
Sets the Host: directive |
AddRobotsTag(path, value) |
Sets X-Robots-Tag header on matching responses |
Allow(path)andCrawlDelay(seconds)without a user-agent apply to the user-agent from the precedingDisallow()call- Duplicate paths and sitemaps are silently ignored
AddRobotsTag(path, value) — X-Robots-Tag headers
Controls how crawlers index specific paths by setting the X-Robots-Tag response header.
Path patterns:
| Pattern | Example | Matches |
|---|---|---|
| * | AddRobotsTag("*", "noindex") | All paths |
| /admin/* | AddRobotsTag("/admin/*", "none") | /admin/, /admin/settings, etc. |
| /secret | AddRobotsTag("/secret", "noindex") | Only that exact path |
The first matching rule wins. The header is not applied to /robots.txt.
services.AddRobotsTxt()
.AddRobotsTag("/admin/*", "noindex, nofollow")
.AddRobotsTag("/private/", "noindex");
Middleware
app.UseRobotsTxt();
/robots.txt— returns the generated content astext/plain; charset=utf-8(GET/HEAD only, other methods return 405). SetsContent-Lengthand optionalCache-Control.- Other paths — if any
AddRobotsTagrule matches the request path, theX-Robots-Tagheader is set on the response before passing through.
appsettings.json
{
"RobotsTxt": {
"Rules": [
{
"UserAgent": "GPTBot",
"Disallow": ["/"]
},
{
"UserAgent": "*",
"Disallow": ["/admin/", "/login/"],
"Allow": ["/login/blabla"],
"CrawlDelay": 10
}
],
"Sitemaps": ["https://example.com/sitemap.xml"],
"Comments": ["Managed by Robots.AspNet"],
"Host": "https://www.example.com",
"CacheMaxAgeSeconds": 86400,
"Tags": [
{ "Path": "/admin/*", "Value": "noindex, nofollow" },
{ "Path": "/private/", "Value": "noindex" }
]
}
}
builder.Services.AddRobotsTxt(builder.Configuration.GetSection("RobotsTxt"))
.Disallow("BadBot", "/");
Output format
Follows RFC 9309 — Robots Exclusion Protocol:
- UTF-8 encoded,
text/plaincontent type - Groups separated by blank lines
- Rules within a group are output in registration order
NuGet
dotnet pack src/Robots.AspNet
Requires no NuGet dependencies beyond the ASP.NET Core shared framework. Package includes symbols (snupkg).
Project structure
Robots.AspNet/
├── README.md
├── Robots.AspNet.slnx
└── src/
├── Robots.AspNet/ # Library
│ ├── IRobotsTxtBuilder.cs
│ ├── RobotsTxtBuilder.cs
│ ├── RobotsTxtMiddleware.cs
│ ├── RobotsTxtOptions.cs
│ ├── RobotsTxtRule.cs
│ ├── RobotsTagRule.cs
│ ├── ServiceCollectionExtensions.cs
│ └── ApplicationBuilderExtensions.cs
└── Robots.AspNet.Tests/ # NUnit tests
├── RobotsTxtBuilderTests.cs
├── RobotsTxtMiddlewareTests.cs
└── ConfigurationBindingTests.cs
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.