[Solved] Parameter with hyphen in Web API 2


try this for url …./api/books?author-id=3&genre-id=5 . It works for all net versions

 [HttpGet]
    public async Task<IHttpActionResult> GetBooks()
    {
       var parameters = GetBooksParameters(HttpContext);
    // ...
   }

    [NonAction]
    private BooksParameters GetBooksParameters(HttpContext httpContext)
    {
        var parameters = new BooksParameters();

        var queryString = httpContext.Request.QueryString.Value;

        foreach (string item in queryString.Split('&'))
        {
            string[] parts = item.Replace("?", "").Split('=');

            switch (parts[0])
            {
                case "author-id":
                    parameters.AuthorId = Convert.ToInt32(parts[1]);
                    break;
                case "book-id":
                    parameters.BookId = Convert.ToInt32(parts[1]);
                    break;

                default:
                    break;
            }
        }
        return parameters;
    }

parameters

 public class BooksParameters
 {
        public int? AuthorId { get; set; }
        public int? BookId { get; set; }
 }

2

solved Parameter with hyphen in Web API 2