Home > Blockchain >  How to pass multiple params to a HttpGet method inside of a C# ApiController?
How to pass multiple params to a HttpGet method inside of a C# ApiController?

Time:01-02

I have this:

public class RuleController : ApiController
{
    private readonly IRuleManager _ruleManager;


    // GET: api/Rule/guid
    [HttpGet]
    public IHttpActionResult GetRule(Guid id)
    {
        var rules = _ruleManager.GetRulesById(id);
        return Ok(rules);
    }

    [HttpGet]
    public async Task<IHttpActionResult> GetRuleNew(string locale, string pno, string modelYear)
    {
      // do cool stuff with my params
    }

}

my route config:

        config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d " });
        config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
        config.Routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
        config.Routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });
        config.Routes.MapHttpRoute(
        name: "ActionApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }

calling my route:

https://localhost:44328/api/rule/GetRuleNew?locale=e?pno=2?modelYear=1

I get:

"Message": "No HTTP resource was found that matches the request URI 'https://localhost:44328/api/rule/GetRuleNew?locale=e?pno=2?modelYear=1'.", "MessageDetail": "No action was found on the controller 'Rule' that matches the request."

What am I missing?

CodePudding user response:

Have you tried using this instead ?

https://localhost:44328/api/rule/GetRuleNew?locale=e&pno=2&modelYear=1 

Basically just use a & (instead of a ?) for the second and subsequent query parameters...

  • Related