Robots.AspNet 1.2.0

dotnet add package Robots.AspNet --version 1.2.0
                    
NuGet\Install-Package Robots.AspNet -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Robots.AspNet" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Robots.AspNet" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Robots.AspNet" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Robots.AspNet --version 1.2.0
                    
#r "nuget: Robots.AspNet, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Robots.AspNet@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Robots.AspNet&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Robots.AspNet&version=1.2.0
                    
Install as a Cake Tool

Robots.AspNet

NuGet CI

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) and CrawlDelay(seconds) without a user-agent apply to the user-agent from the preceding Disallow() 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 as text/plain; charset=utf-8 (GET/HEAD only, other methods return 405). Sets Content-Length and optional Cache-Control.
  • Other paths — if any AddRobotsTag rule matches the request path, the X-Robots-Tag header 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/plain content 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
1.2.0 317 7/30/2026
1.1.1 102 7/30/2026
1.1.0 105 7/30/2026
1.0.0 100 7/30/2026