5

I have WebApi baed web service like:

public class ControllerName : ApiController
{
    [Route("api/ControllerName/{p1}/{p2}/{p3}/{p4}/{p5}")]
    public string GetItemByNameAndId(string p1, string p2, string p3, string p4, string p5)
    {

    }
}

This type request is working:

host/api/ControllerName/1/2/3/4/5

But I want this type of request:

host/api/ControllerName/&p1=1&p2=2&p3=3&p4=4&p5=5

and I receive error like:

A potentially dangerous Request.Path value was detected from the client (&). Which steps must be followed? Any changes in config files, any attributes etc.

Thanks for your time.

4
  • cor, that's a broad question.. What have you done so far and why do you think it doesn't work? Commented Dec 9, 2018 at 17:58
  • But, you've declared the route to expect /1/2/3/4/5 Commented Dec 9, 2018 at 20:14
  • 1
    stackoverflow.com/questions/41969112/… Commented Dec 9, 2018 at 20:15
  • the error is because you first param has &p1 and not ?p1 Commented Feb 23, 2023 at 18:26

2 Answers 2

9

If you do not want the first type of request

host/api/ControllerName/1/2/3/4/5

Then remove them from the route template

[RoutePrefix("api/ControllerName")]
public class ControllerName : ApiController {
    //GET api/ControllerName?p1=1&p2=2&p3=3&p4=4&p5=5
    [HttpGet]
    [Route("")]
    public IHttpActionResult GetItemByNameAndId(string p1, string p2, string p3, string p4, string p5) {
        //...
        return Ok(someResult);
    }
}

So that the query string version will map to the controller action.

Sign up to request clarification or add additional context in comments.

Comments

0

too many parameters is not a good design, and it can be refact to a parameter class.

public class ParamClass {
    public string P1 { get; set; }
    public string P2 { get; set; }
    public string P3 { get; set; }
    public string P4 { get; set; }
    public string P5 { get; set; }
}

and in your api controller, use it like this:

//GET api/ControllerName?host/api/ControllerName?p1=1&p2=2&p3=3&p4=4&p5=5
[HttpGet]
[Route("")]
public IHttpActionResult GetItemByNameAndId([FromUri(PreFix="")] ParamClass param) {
    //...
    return Ok(someResult);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.