Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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&#8217;.",
"MessageDetail": "No action was found on the controller ‘Rule’ that
matches the request."

What am I missing?

>Solution :

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…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading