MVC AttributeRouting用GET - 返回405 - 不允许的方法 [英] MVC AttributeRouting With GET - Returning 405 - Method Not Allowed

查看:143
本文介绍了MVC AttributeRouting用GET - 返回405 - 不允许的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚工作的一个新的控制器的操作方法和我有点困惑,为什么我看到一个405。

I've just been working on a new controller action method and I'm a little confused why I'm seeing a 405.

我已经定义我的API的几个都会归属方法,他们都按预期。举一个例子能正常工作:

I have defined several GET attributed methods on my API already and they all function as expected. For an example this works fine:

    [GET("entries/{page}"), JsonExceptionFilter]
    public HttpResponseMessage GetEntries(int page)

然而,我这样定义新方法:

Yet my new method defined like this:

    [GET("search/{searchTerm}/{page}"), JsonExceptionFilter]
    public HttpResponseMessage Search(string searchTerm, int page)

返回一个405

Is returning a 405

如果我访问的API的routes.axd网址我可以看到像这样在表中的条目:

If I visit the routes.axd url on the API I can see an entry in the table like this:

GET, HEAD, OPTIONS  users/search/{searchTerm}/{page}

这一切看起来是正确的。在客户端,我使用这两个请求同样的方法使用的HttpClient:

This all looks correct. On the client side I'm using the same approach on both requests using an HttpClient:

var response = httpClient.GetAsync(ApiRootUrl + "users/search/" + searchTerm + "/" + page).Result;

从提琴手运行GET也返回405。

Running a get from Fiddler also returns a 405.

连看在响应中RequestMessage看起来是正确的:

Even looking at the RequestMessage in the response looks correct:

"{Method: GET, RequestUri: 'http://localhost:51258/users/search/jam/0'"

完全难住了这一点。

Totally stumped on this.

还有什么我可以尝试也许阐明了什么错误一些轻?

What else can I try to maybe shed some light on what is going wrong?

推荐答案

您将需要装饰与一个叫做多个属性的搜索行动 System.Web.Http.HttpGetAttribute 。对于这背后的原因,你可以在下面的帖子看看我的回答是:

You would need to decorate the Search action with one more attribute called System.Web.Http.HttpGetAttribute. For the reason behind this, you can take a look at my answer in the below post:

<一个href=\"http://stackoverflow.com/questions/17196731/405-when-using-attributerouting-putattribute-unless-i-also-include-httpputattrib/17197131#17197131\">405使用AttributeRouting.PUTAttribute时,除非我还包括HttpPutAttribute

这篇关于MVC AttributeRouting用GET - 返回405 - 不允许的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆