Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Expose OpenSearch Endpoint with an ASP.NET Core Controller Action

Expose OpenSearch Endpoint with an ASP.NET Core Controller Action
January 5, 2020 3 min read BEN ABT
.NET ASP.NET Core

This article was written in 2020. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.

OpenSearch

OpenSearch is a standard that has existed for about 15 years and is used by search engines and Browsers.

This standard was developed by Amazon and is used today, for example, for browsers to have a search field as the start page, for a search bar to be directly in the browser or for a search bar to be embedded directly in Google search hits for the respective web page.

Structure

The OpenSearch definition is in XML and must then be linked linked in header of the website.

For example: Stackoverflow

HTML
 1<html class="html__responsive">
 2
 3    <head>
 4
 5        <title>Stack Overflow - Where Developers Learn, Share, &amp; Build Careers</title>
 6        <link rel="shortcut icon" href="..">
 7        <link rel="apple-touch-icon" href="...">
 8        <link rel="image_src" href="..."> 
 9
10        <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
11        ..
12        ..
13    </head>

So here we see the HTML head area and the link entry for the OpenSearch definition XML.

This tells the browser or a search bot that the website supports OpenSearch and the definition for it is available at /opensearch.xml.

XML
 1<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
 2    <ShortName>Stack Overflow</ShortName>
 3    <Description>
 4        Search Stack Overflow: Q&A for professional and enthusiast programmers
 5    </Description>
 6    <InputEncoding>UTF-8</InputEncoding>
 7    <Image width="16" height="16" type="image/x-icon">
 8        https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d
 9    </Image>
10    <Url type="text/html" method="get" template="http://stackoverflow.com/search?q={searchTerms}"/>
11</OpenSearchDescription>

The definition itself is relatively lean; it contains only the name, the description, an image and a link to the search with a placeholder for the search input.

ASP.NET OpenSearch Endpoint

In principle, you could simply deliver this as a static page, which would have the disadvantage that you would have to save the URLs permanently.

This is less tragic for relative URLs; however, in a non-fixed environment, it is not so easy.
With ASP.NET Core, however, this is relatively easy to implement with the XDocument class.

C#
 1[Route("opensearch.xml", Route="OpenSearch"]
 2public IActionResult OpenSearch()
 3{
 4    XNamespace nsOs = "http://a9.com/-/spec/opensearch/1.1/";
 5
 6    XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
 7        new XElement(nsOs + "OpenSearchDescription",
 8            new XElement(nsOs + "ShortName", "Your ShortName"),
 9            new XElement(nsOs + "Description", "Your Description"),
10            new XElement(nsOs + "InputEncoding", "UTF-8"),
11            new XElement(nsOs + "Image",
12                new XAttribute("width", 64),
13                new XAttribute("height", 64),
14                new XAttribute("type", "image/x-icon"),
15                $"{Url.Content("~/favicon.ico")}"
16            ),
17            new XElement(nsOs + "Url",
18                new XAttribute("type", "text/html"),
19                new XAttribute("method", "get"),
20                  new XAttribute("template",  $"{Url.RouteUrl("OpenSearchQueryEndpoint")}?query=") 
21            )
22        )
23    );
24
25    return File(Encoding.UTF8.GetBytes(doc.ToString()), "text/xml");
26}

To avoid to generate the XML on every request we can simply use the InMemory Caching . This improves the performance a lot, but we remain flexible.

C#
 1[Route("opensearch.xml", Route="OpenSearch"]
 2public IActionResult OpenSearch()
 3{
 4    if (!_cache.TryGetValue("OpenSearch", out byte[] data))
 5    {
 6        XNamespace nsOs = "http://a9.com/-/spec/opensearch/1.1/";
 7
 8        XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
 9            new XElement(nsOs + "OpenSearchDescription",
10                new XElement(nsOs + "ShortName", "Your ShortName"),
11                new XElement(nsOs + "Description", "Your Description"),
12                new XElement(nsOs + "InputEncoding", "UTF-8"),
13                new XElement(nsOs + "Image",
14                    new XAttribute("width", 64),
15                    new XAttribute("height", 64),
16                    new XAttribute("type", "image/x-icon"),
17                    $"{Url.Content("~/favicon.ico")}"
18                ),
19                new XElement(nsOs + "Url",
20                    new XAttribute("type", "text/html"),
21                    new XAttribute("method", "get"),
22                    new XAttribute("template",  $"{Url.RouteUrl("OpenSearchQueryEndpoint")}?query=") 
23                )
24            )
25        );
26
27        data = Encoding.UTF8.GetBytes(doc.ToString());
28
29        _cache.Set("OpenSearch", data, TimeSpan.FromDays(7));
30    }
31    return File(data, "text/xml");
32}

For the head area only the following is missing

HTML
<link rel="search" type="application/opensearchdescription+xml" title="Your Website" href="@(Url.RouteUrl("OpenSearch"))">

Browsers then automatically recognize that the website has an endpoint for OpenSearch.

The same, of course, applies to search engine bots, which means that after a few hours or even days the search bar will appear in Google’s hit list.

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 12 min read

Problem Details in ASP.NET Core and .NET 11

builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026 - 11 min read

Resilience in .NET with Microsoft.Extensions.Resilience

Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact