gpt4 book ai didi

c# - ASP.NET/MVC : The requested resource does not support http method 'GET'

转载 作者:行者123 更新时间:2023-11-30 21:55:24 27 4
gpt4 key购买 nike

我在 ApiController 中定义了一个方法。当方法定义如下:

public IEnumerable<QuestionResponse> Get() 

一切都很好。但是我想将参数传递给该方法但是当我将该方法定义为

public IEnumerable<QuestionResponse> Get(int parid) 

响应是 405“请求的资源不支持 http 方法‘GET’”。

当没有为该方法定义参数时,这两个 url 都会被路由到该方法:

/api/questionresponse/224809
/api/questionresponse

但是当我为该方法定义一个参数时,这两个 url 都会导致 405。

帮助?


前世今生

最佳答案

如果你检查路由,你可以看到默认模板使用参数 {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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com