我在 ApiController 中定义了一个方法。当方法定义如下时:
public IEnumerable<QuestionResponse> Get()
一切都很好。但是我想将参数传递给该方法,但是当我将该方法定义为
public IEnumerable<QuestionResponse> Get(int parid)
响应是 405“请求的资源不支持 http 方法 'GET'”。
如果没有为该方法定义参数,则这两个 url 都会路由到该方法:
/api/questionresponse/224809
/api/questionresponse
但是当我为该方法定义参数时,这两个 url 都会导致 405 。
帮忙?
FWIW
- 该请求是由同一项目内提供的页面上的某些 jquery 发出的
- 我迄今为止所做的工作都是基于此文本 http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api .
- 我在这里描述的结果都是在 Visual Studio 2015 中运行时的结果。
最佳答案
如果您检查路由,您可以看到默认模板使用参数{id}
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
因此您需要将 Controller 签名更改为
public IEnumerable<QuestionResponse> Get(int id)
或者编写一个自定义路由来支持参数parid
关于c# - ASP.NET/MVC : The requested resource does not support http method 'GET' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32324146/