Reputation: 459
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.
Upvotes: 5
Views: 18749
Reputation: 247008
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.
Upvotes: 9
Reputation: 3050
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);
}
Upvotes: 0