简单的控制器这需要POST未找到 [英] Simple controller which takes POST is not found

查看:189
本文介绍了简单的控制器这需要POST未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些previous问题询问的问题,因为我更新MVC4的WebAPI公测RC的帮助。我为了现在大多数,但这里有一个我无法找出原因呢。

对于这个简单的控制器我有一个它接受一个POST和一个接受GET。当我试图运行那些来自HTML表单发送请求,只有GET控制器被发现,而POST人会回到我下面的错误。

  {
  消息:没有HTTP资源发现,请求URI的http://本地主机/的WebAPI / API /播放/测试相匹配。
  MessageDetail:没有采取任何控制器上的播放,这个名字'测试'匹配找到。
}

为什么找不到POST控制器?

控制器

 公共类PlayController:ApiController
{
    [HttpPost] //未找到
    公共字符串测试(字符串输出)
    {
        返回输出;
    }    [HTTPGET] //作品
    公共字符串的Test2(字符串输出)
    {
        返回输出;
    }
}

HTML表单

 <形式的行动=HTTP://本地主机/的WebAPI / API /播放/测试方法=后>
<输入类型=文本名称=输出/>
<输入类型=提交名称=提交/>
< /表及GT;<形式的行动=HTTP://本地主机/的WebAPI / API /播放/ test2的方法=获取>
<输入类型=文本名称=输出/>
<输入类型=提交名称=提交/>
< /表及GT;


解决方案

Web.API时要张贴简单的价值观是有点挑剔。

您需要使用 [FromBody] 属性,以表示该值不是从URL中来,但在发布的数据:

[HttpPost]
公共字符串测试([FromBody]字符串输出)
{
    返回输出;
}

随着这一变化,你不会得到404了,但输出将始终为空,因为Web.Api requries中的special格式(查找发送简单类型部分):


  

其次,客户端需要具有以下格式发送的值:


  
  

=值


  
  

具体而言,名称/值对的名称部分必须是空的一个简单的类型。并非>所有浏览器都支持这种为HTML表单,
  但你创建脚本格式...


所以,建议你应该创建一个模型类型:

公共类为MyModel
{
    公共字符串输出{搞定;组; }
}[HttpPost]
公共字符串测试(为MyModel模型)
{
    返回model.Output;
}

然后,它会与你的样品工作,320交织,而不对矫正你的看法。

I've made some previous question asking for the help with the problems since I updated MVC4 webapi beta to RC. I got most in order now, but here's one I cannot figure out the reason for yet.

For this simple controller I have one which accepts a POST and one that accepts GET. When I try to run those by sending request from a HTML form, only the GET controller is found while the POST one will return me the following error.

{
  "Message": "No HTTP resource was found that matches the request URI 'http://localhost/webapi/api/play/test'.",
  "MessageDetail": "No action was found on the controller 'Play' that matches the name 'test'."
}

Why is the POST controller not found?

Controllers

public class PlayController : ApiController
{
    [HttpPost]  // not found
    public string Test(string output)
    {
        return output;
    }

    [HttpGet]  // works
    public string Test2(string output)
    {
        return output;
    }
}

HTML form

<form action="http://localhost/webapi/api/play/test" method="post">
<input type="text" name="output" />
<input type="submit" name="submit" />
</form>

<form action="http://localhost/webapi/api/play/test2" method="get">
<input type="text" name="output" />
<input type="submit" name="submit" />
</form>

解决方案

Web.API is a little bit picky when you want to post "simple" values.

You need to use the [FromBody] attribute to signal that the value is not coming from the URL but from the posted data:

[HttpPost]
public string Test([FromBody] string output)
{
    return output;
}

With this change you won't get 404 anymore but output will be always null, because Web.Api requries the posted values in special format (look for the Sending Simple Types section):

Second, the client needs to send the value with the following format:

=value

Specifically, the name portion of the name/value pair must be empty for a simple type. Not >all browsers support this for HTML forms, but you create this format in script...

So recommend that you should create a model type:

public class MyModel
{
    public string Output { get; set; }
}

[HttpPost]
public string Test(MyModel model)
{
    return model.Output;
}

Then it will work with your sample froms without modifing your views.

这篇关于简单的控制器这需要POST未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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