How can I overload ASP.NET MVC Actions based on the accepted HTTP verbs?

前端 未结 4 1179
别那么骄傲
别那么骄傲 2021-01-01 12:14

Wanted to use the same URL for a GET/PUT/DELETE/POST for a REST based API, but when the only thing different about the Actions is which HTTP verbs it accepts, it considers t

4条回答
  •  温柔的废话
    2021-01-01 12:40

    Another option is to have a single method that accepts all and distinguishes between HttpMethod and calls the appropriate code from there. E.g.

                string httpMethod = Request.HttpMethod.ToUpperInvariant();
    
                switch (httpMethod)
                {
                    case "GET":
                        return GetResponse();
    
                    case "POST":
                        return PostResponse();
    
                    default:
                        throw new ApplicationException(string.Format("Unsupported HttpMethod {0}.", httpMethod));
                }
    

提交回复
热议问题