简体   繁体   中英

How to send empty route parameter to a web API?

I am calling a web api using this code

public class SearchController : ApiController
{
    [Route("api/SearchController/{paramOne}/{paramTwo}")]
    public string Get(string paramOne, string paramTwo)
    {
        return "The [Route] with multiple params worked.\nP1 : " + paramOne + "    P2 : " + paramTwo;
    }
}

It works fine.

if I call https://localhost:44319/api/SearchSelectController/Mike/Tom

I get this

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<link type="text/css" rel="stylesheet" id="dark-mode-custom-link"/>
<link type="text/css" rel="stylesheet" id="dark-mode-general-link"/>
<style lang="en" type="text/css" id="dark-mode-custom-style"/>
<style lang="en" type="text/css" id="dark-mode-native-style"/>
The [Route] with multiple params worked. P1 : Mike P2 : Tom
</string>

but if I am sending empty string for the first or 2nd parameter

https://localhost:44319/api/SearchSelectController//Tom

I get this error

<Error>
<link type="text/css" rel="stylesheet" id="dark-mode-custom-link"/>
<link type="text/css" rel="stylesheet" id="dark-mode-general-link"/>
<style lang="en" type="text/css" id="dark-mode-custom-style"/>
<style lang="en" type="text/css" id="dark-mode-native-style"/>
<Message>No HTTP resource was found that matches the request URI 'https://localhost:44319/api/SearchSelectController/Tom'.</Message>
<MessageDetail>No type was found that matches the controller named 'SearchSelectController'.</MessageDetail>
</Error>

So I wonder how can I send empty string as parameter for a web API?

In this case only one solution exists. Use query parameter like below:

public class SearchController : ApiController
{
    [Route("api/SearchController")]
    public string Get([FromQuery] ModelWithTwoParam model)
    {
        return "The [Route] with multiple params worked.\nP1 : " + model.paramOne + "    P2 : " + model.paramTwo;
    }
}

see documents:

[FromQuery]

It is not possible to send empty route parameters, because they break the URL-schema.
To solve your problem you can send them as GET-Parameters. So your method in the Controller looks like:

[Route("api/SearchController")]
public string Get(string paramOne = null, string paramTwo= null)
  {
    return "The [Route] with multiple params worked.\nP1 : " + paramOne + "    P2 : " + paramTwo;
  }

The URLs look like this:

https://localhost:44319/api/SearchSelectController?paramOne=Mike&paramTwo=Tom
https://localhost:44319/api/SearchSelectController?paramTwo=Tom
https://localhost:44319/api/SearchSelectController?paramOne=Mike

If each input parameter may be null or any combination of these, tell the action method that each of these may be null. The following code also supports null values.

[Route("api/SearchController/{paramOne?}/{paramTwo?}")]
public string Get(string paramOne = null, string paramTwo = null)
{
    return "The [Route] with multiple params worked.\nP1 : " + paramOne + "    P2 : " + paramTwo;
}

Or another method

set the WebApiConfig.cs file as follows. If you do not have this file, create it in the App_Start folder.

public static class WebApiConfig
{
     public static void Register(HttpConfiguration config)
     {
       config.MapHttpAttributeRoutes();

       config.Routes.MapHttpRoute(
            name: "myroute",
            routeTemplate: "api/SearchController/{paramOne}/{paramTwo}",
            defaults: new { controller = "Search", action = "Get", paramOne = RouteParameter.Optional, paramTwo = RouteParameter.Optional }
        );

         config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional}
        );
    }
}

and register above class in global.cs

protected void Application_Start()
{   /................
    GlobalConfiguration.Configure(WebApiConfig.Register);
    /................
}

Finally the api method

public string Get(string paramOne = null, string paramTwo = null)
{
    return "The [Route] with multiple params worked.\nP1 : " + paramOne + "    P2 : " + paramTwo;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM