Robots.AspNet 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Robots.AspNet --version 1.0.0
                    
NuGet\Install-Package Robots.AspNet -Version 1.0.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.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Robots.AspNet" Version="1.0.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.0.0
                    
#r "nuget: Robots.AspNet, 1.0.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.0.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.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Robots.AspNet&version=1.0.0
                    
Install as a Cake Tool

Robots.AspNet

A lightweight ASP.NET Core library that serves a dynamic robots.txt file via middleware, with a fluent builder API and appsettings.json support.

Quick start

1. Install

dotnet add package Robots.AspNet

2. Configure

In Program.cs, use the fluent builder to define your rules:

using Robots.AspNet;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRobotsTxt()
    .Disallow("GPTBot", "/")
    .Disallow("*", "/admin/", "/login/")
    .Allow("/login/blabla")
    .Sitemap("https://example.com/sitemap.xml");

var app = builder.Build();

app.UseRobotsTxt();
app.Run();

That's it. A GET /robots.txt now returns:

User-agent: GPTBot
Disallow: /

User-agent: *
Disallow: /admin/
Disallow: /login/
Allow: /login/blabla

Sitemap: https://example.com/sitemap.xml

API

AddRobotsTxt() returns IRobotsTxtBuilder

services.AddRobotsTxt()

Three overloads:

Overload Use case
AddRobotsTxt() Configure entirely via the fluent API
AddRobotsTxt(IConfiguration) Seed from appsettings.json, then override with fluent calls
AddRobotsTxt(Action<RobotsTxtOptions>) Configure via a delegate

IRobotsTxtBuilder fluent methods:

IRobotsTxtBuilder Disallow(string userAgent, params string[] paths)
IRobotsTxtBuilder Allow(string path)
IRobotsTxtBuilder Allow(string userAgent, string path)
IRobotsTxtBuilder Sitemap(string url)
  • Allow(path) applies to the user-agent from the preceding Disallow() call
  • Allow(userAgent, path) explicitly targets a user-agent
  • Duplicate paths and sitemaps are silently ignored

Middleware

app.UseRobotsTxt();

Place it anywhere in the pipeline. Requests to /robots.txt (case-insensitive) return the generated content as text/plain; charset=utf-8. All other requests pass through.

appsettings.json

{
  "RobotsTxt": {
    "Rules": [
      {
        "UserAgent": "GPTBot",
        "Disallow": ["/"]
      },
      {
        "UserAgent": "*",
        "Disallow": ["/admin/", "/login/"],
        "Allow": ["/login/blabla"]
      }
    ],
    "Sitemaps": ["https://example.com/sitemap.xml"]
  }
}

Then in Program.cs:

builder.Services.AddRobotsTxt(builder.Configuration.GetSection("RobotsTxt"))
    .Disallow("BadBot", "/"); // merge additional rules

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

The package includes symbols (snupkg) and requires no NuGet dependencies beyond the ASP.NET Core shared framework.

Project structure

Robots.AspNet/
├── README.md
├── Robots.AspNet.slnx
└── src/
    ├── Robots.AspNet/           # Library
    │   ├── IRobotsTxtBuilder.cs
    │   ├── RobotsTxtBuilder.cs
    │   ├── RobotsTxtMiddleware.cs
    │   ├── RobotsTxtOptions.cs
    │   ├── RobotsTxtRule.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 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.
  • 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 623 7/30/2026
1.1.1 122 7/30/2026
1.1.0 120 7/30/2026
1.0.0 117 7/30/2026