[c#] Web API optional parameters

I have a controller with the following signature:

[Route("products/filter/{apc=apc}/{xpc=xpc}/{sku=sku}")]
public IHttpActionResult Get(string apc, string xpc, int? sku)
{ ... }

I call this method with following URIs:

  • ~/api/products/filter?apc=AA&xpc=BB
  • ~/api/products/filter?sku=7199123

The first URI works without issue. The second one has a strange side effect. Even though the default values for apc and xpc should be null when not provided, the parameters are actually their names. I can overcome this by adding the additional logic:

apc = (apc == "apc") ? null : apc;
xpc = (xpc == "xpc") ? null : xpc;

This seems like a hack, and would be problematic if value passed was ever equal to the parameter name.

Is there a way to define the Route without this side effect?

This question is related to c# asp.net-web-api asp.net-web-api2

The answer is


I figured it out. I was using a bad example I found in the past of how to map query string to the method parameters.

In case anyone else needs it, in order to have optional parameters in a query string such as:

  • ~/api/products/filter?apc=AA&xpc=BB
  • ~/api/products/filter?sku=7199123

you would use:

[Route("products/filter/{apc?}/{xpc?}/{sku?}")]
public IHttpActionResult Get(string apc = null, string xpc = null, int? sku = null)
{ ... }

It seems odd to have to define default values for the method parameters when these types already have a default.


Sku is an int, can't be defaulted to string "sku". Please check Optional URI Parameters and Default Values


you need only set default value to parameters(you do not need the Route attribute):

public IHttpActionResult Get(string apc = null, string xpc = null, int? sku = null)
{ ... }

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to asp.net-web-api

Entity Framework Core: A second operation started on this context before a previous operation completed FromBody string parameter is giving null How to read request body in an asp.net core webapi controller? JWT authentication for ASP.NET Web API Token based authentication in Web API without any user interface Web API optional parameters How do I get the raw request body from the Request.Content object using .net 4 api endpoint How to use a client certificate to authenticate and authorize in a Web API HTTP 415 unsupported media type error when calling Web API 2 endpoint The CodeDom provider type "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider" could not be located

Examples related to asp.net-web-api2

CORS: credentials mode is 'include' FromBody string parameter is giving null Web API optional parameters HTTP 415 unsupported media type error when calling Web API 2 endpoint Asp.Net WebApi2 Enable CORS not working with AspNet.WebApi.Cors 5.2.3 How to implement oauth2 server in ASP.NET MVC 5 and WEB API 2 Pass multiple complex objects to a post/put Web API method Where can I find a NuGet package for upgrading to System.Web.Http v5.0.0.0? How to get base URL in Web API controller? No connection could be made because the target machine actively refused it?