---
title: "ASP.NET Core Hidden Gem - QueryHelpers - Muhammad Rehan Saeed"
description: "How to build a URL containing dynamic query arguments using the ASP.NET Core QueryHelpers.AddQueryString method. A hidden gem in ASP.NET Core."
canonical_url: "https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/"
last_updated: "2018-07-14T00:00:00+00:00"
meta:
  author: "Muhammad Rehan Saeed"
  keywords: "ASP.NET Core,C#,QueryHelpers,URL"
  "og:description": "How to build a URL containing dynamic query arguments using the ASP.NET Core QueryHelpers.AddQueryString method. A hidden gem in ASP.NET Core."
  "og:image": "https://rehansaeed.com/_og/s/o_78875af83b775fd6.png"
  "og:image:alt": "ASP.NET Core Hidden Gem - QueryHelpers - Muhammad Rehan Saeed"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "ASP.NET Core Hidden Gem - QueryHelpers"
  "twitter:image": "https://rehansaeed.com/_og/s/o_78875af83b775fd6.png"
  "twitter:image:alt": "ASP.NET Core Hidden Gem - QueryHelpers - Muhammad Rehan Saeed"
---

> Index of every page on this site: [llms.txt](https://rehansaeed.com/llms.txt)

# [ASP.NET Core Hidden Gem - QueryHelpers](https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/)

Posted 14 July 2018 - **1 min read**

![Microsoft .NET](https://rehansaeed.com/_ipx/f_png&s_2560x1440/images/hero/Microsoft-.NET-1366x768.png)

I discovered a hidden gem in ASP.NET Core a couple of weeks ago that can help to build up and parse URL's called `QueryHelpers`. Here's how you can use it to build a URL using the `AddQueryString` method:

```cs
var queryArguments = new Dictionary<string, string>()
{
    { "static-argument", "foo" },
};

if (someFlagIsEnabled)
{
    queryArguments.Add("dynamic-argument", "bar");
}

string url = QueryHelpers.AddQueryString("/example/path", queryArguments);
```

Notice that there are no question marks or ampersands in sight. Where this really shines is when you want to add multiple arguments and then need to write code to work out whether to add a question mark or ampersand.

It's also worth noting that the values of the query arguments are URL encoded for you too. The type also has a `ParseQuery` method to parse query strings but that's less useful to us as ASP.NET Core controllers do that for you.

Finally, .NET also has a type called `UriBuilder` that you should know about. It's more geared towards building up a full URL, rather than a relative URL as I'm doing above. It has a `Query` property that you can use to set the query string but it's only of type string, so much less useful than `QueryHelpers.AddQueryString`.

## Web Mentions

[What's this?](https://en.wikipedia.org/wiki/Webmention)

### 0 Linked

### 0 Replies

## GitHub Comments

### 3 Comments

[Comment on GitHub](https://github.com/RehanSaeed/rehansaeed.github.io/issues/55)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![noor](https://camo.githubusercontent.com/27d9e4893464fce346f12eebb48ffcdeb102a35bb129af16e3380df5d67cb94b/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f33323836346261396232383431613066346436643532333231663864333139613f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/27d9e4893464fce346f12eebb48ffcdeb102a35bb129af16e3380df5d67cb94b/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f33323836346261396232383431613066346436643532333231663864333139613f733d393626643d6d6d26723d67) **noor** commented on **2018-10-23 20:40:00**

well that's cool

[![zaLTys](https://avatars.githubusercontent.com/u/2812503?v=4)](https://github.com/zaLTys)

### [zaLTys](https://github.com/zaLTys)

14 February 2021 (edited 14 February 2021)

What about an array? In the query string like : http://www.asdf.url/path/to/query?id=1&id=2&id=3

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed)

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

15 February 2021

[@zaLTys](https://github.com/zaLTys) Good question! I suppose that wouldn't work, given that they use a dictionary. You'd have to fallback to doing it manually.

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://rehansaeed.com/#website","@type":"WebSite","description":"Software Developer at Microsoft, YouTuber, Open Source Contributor and Blogger","inLanguage":"en-GB","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/","author":{"@id":"https://rehansaeed.com/#identity"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://rehansaeed.com/?search={search_term_string}"},"query-input":"required name=search_term_string"}],"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/#webpage","@type":"WebPage","dateModified":"2018-07-14T00:00:00+00:00","datePublished":"2018-07-14T00:00:00+00:00","description":"How to build a URL containing dynamic query arguments using the ASP.NET Core QueryHelpers.AddQueryString method. A hidden gem in ASP.NET Core.","name":"ASP.NET Core Hidden Gem - QueryHelpers","url":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/","breadcrumb":{"@id":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/#breadcrumb"},"isPartOf":{"@id":"https://rehansaeed.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/"]}]},{"@id":"https://rehansaeed.com/#identity","@type":"Person","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/about/","image":{"@id":"https://rehansaeed.com/#/schema/image/1"},"sameAs":["https://github.com/RehanSaeed","https://www.linkedin.com/in/muhammad-rehan-saeed","https://stackoverflow.com/users/1212017/muhammad-rehan-saeed","https://www.youtube.com/c/MuhammadRehanSaeed","https://www.twitch.tv/rehansaeeduk"]},{"@id":"https://rehansaeed.com/#publisher","@type":"Organization","logo":"https://rehansaeed.com/images/schema/Publisher-600x60.png","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com"},{"@id":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/#article","@type":"Article","dateModified":"2018-07-14T00:00:00+00:00","datePublished":"2018-07-14T00:00:00+00:00","description":"How to build a URL containing dynamic query arguments using the ASP.NET Core QueryHelpers.AddQueryString method. A hidden gem in ASP.NET Core.","headline":"ASP.NET Core Hidden Gem - QueryHelpers","inLanguage":"en-GB","url":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/","author":{"@id":"https://rehansaeed.com/#identity"},"image":[{"@type":"ImageObject","url":"https://rehansaeed.com/images/hero/Microsoft-.NET-1366x768.png","caption":"Microsoft .NET","width":"1366","height":"768"}],"isPartOf":{"@id":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/#webpage"},"keywords":["ASP.NET Core","C#","QueryHelpers","URL"],"mainEntityOfPage":{"@id":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/#webpage"},"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Blog","item":"https://rehansaeed.com/","position":1},{"@type":"ListItem","name":"ASP.NET Core Hidden Gem - QueryHelpers","item":"https://rehansaeed.com/asp-net-core-hidden-gem-queryhelpers/","position":2}]},{"@id":"https://rehansaeed.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png","inLanguage":"en-GB","url":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png"}]}
```

## Sitemap

See the full [sitemap](https://rehansaeed.com/sitemap.md) for all pages.
