Return an error when invalid parameters are specified in ASP Web API

前端 未结 3 1221
长情又很酷
长情又很酷 2021-01-26 15:03

I\'m creating an API using C# and ASP.NET Web API and I want it to return an error when a parameter is used that isn\'t recognised.

For example:

/api/Eve         


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-01-26 16:00

    try strongly typed actions like this

      public string Get()
            {
                return "I'm alive empty";
            }
    
      public string Get([FromUri] int id)
            {
                return "I'm alive";
            }
    

    So normal call will return I'm alive or I'm alive empty

      http://localhost:1578/api/alive?id=1 //OR
      http://localhost:1578/api/alive
    

    But if you try to call it like this

    http://localhost:1578/api/alive?blablabla=1
    

    You will occure this error The requested resource does not support http method 'GET'.

提交回复
热议问题