邮政int型方法的一部分,但得不到任何HTTP资源发现,请求URI匹配错误 [英] Post int as part of method but get No HTTP resource was found that matches the request URI error

查看:322
本文介绍了邮政int型方法的一部分,但得不到任何HTTP资源发现,请求URI匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个MVC的WebAPI控制器,​​它接受一个I​​D,它创建了一个记录在数据库中,然后返回。不过,我不断收到一个错误。

I am trying to create a MVC WebAPI controller, which takes in an id, which it creates a record with in the database and then returns. However, I keep getting an error.

在我testAPI控制器我有:

In my testAPI controller I have:

[HttpPost]
public HttpResponseMessage OpenSession(int id)
{
    //Logic of post in here never gets hit
}

然而,当我尝试后的API,我得到以下响应:

However when I try and post to the API I get the following response:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:54388/api/testAPI/OpenSession/'.","MessageDetail":"No action was found on the controller 'testAPI' that matches the request."}

我已经改变了我的路由:

I have changed my routing to:

config.Routes.MapHttpRoute(
  name: "DefaultApi",
  routeTemplate: "api/{controller}/{action}/"
);

我试图邮寄到:的http://本地主机:54388 / API / testAPI /的openSession / 与有效载荷中的id值。不过,我认为这是在URL中期待它 - 能有人请指出我要去的地方错了

I am trying to post to: http://localhost:54388/api/testAPI/OpenSession/ with the value id in the payload. However I think it is expecting it in the URL - can someone please point out where I am going wrong.

推荐答案

的WebAPI是有问题的,当你尝试发布只有一个参数。我记得我有同样的问题。有很多方法来排序了这一点,但每次的工作之一,就是有一个模型,而不是一个int:

WebApi is problematic when you try to POST only a single parameter. I remember I had the same problem. There are many ways to sort this out, but the one that works every time is to have a model instead of an int:

[HttpPost]
public HttpResponseMessage OpenSession(OpenSessionParameters parameters)
{
    //Logic of post in here never gets hit
}

public class OpenSessionParameters
{
    public int Id { get; set; }
}

或者,如果你坚持不上课的,你可以试试这个:的 http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/ 结果
我已经使用这个了一下,但最终替换所有的 [FromBody] 参数车型 - 工作更可靠。

Or if you insist on not having a class, you can try this: http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/
I have used this for a bit, but ended up replacing all of [FromBody] arguments to models - works more reliable.

这篇关于邮政int型方法的一部分,但得不到任何HTTP资源发现,请求URI匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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