本文介绍了POST JSON使用MVC 4 API控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

  $阿贾克斯({
        键入:POST,
        网址:/ API /幻灯片
        缓存:假的,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据:{标题:fghfdhgfdgfd}
        数据类型:JSON

这是这是我的CONTROLER:

 公共类SlideController:ApiController
{    // POST / API /幻灯片
    公共无效后(字符串标题)
    {
    }

当我运行code和调用/ API /幻灯片时,[标题]没有数据,是空。

我如何发布JSON的API控制器?

  POST http://127.0.0.2:81/api/slide HTTP / 1.1
主持人:127.0.0.2:81
连接:保持活动
内容长度:18
原产地:http://127.0.0.2:81
的X请求 - 由于:XMLHtt prequest
用户代理:Mozilla的/ 5.0(Windows NT的6.1; WOW64)为AppleWebKit / 537.1(KHTML,像壁虎)的Chrome / Safari浏览器21.0.1180.89 / 537.1
内容类型:应用程序/ JSON的;字符集= UTF-8
接受:应用/ JSON,文本/ javascript中,* / *; Q = 0.01
引用者:http://127.0.0.2:81/
接受编码:gzip,紧缩,SDCH
接受语言:EN-US,EN; Q = 0.8
接收字符集:ISO-8859-1,UTF-8,Q = 0.7 *; Q = 0.3标题= fghfdhgfdgfd


解决方案

定义视图模型:

 公共类SlideViewModel
{
    公共字符串名称{搞定;组; }
}

再有你的控制器动作借此视图模型作为参数:

 公共类SlideController:ApiController
{
    // POST / API /幻灯片
    公共无效后(SlideViewModel模型)
    {
        ...
    }
}

最后调用操作:

  $。阿贾克斯({
    输入:POST,
    网址:'/ API /幻灯片,
    缓存:假的,
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据:JSON.stringify({标题:fghfdhgfdgfd})
    成功:函数(){
        ...
    }
});

的原因是简单的类型,如串从URI约束。我还请你读following文章有关Web API中的模型绑定。

I have this code:

   $.ajax({


        type: "POST",
        url: "/api/slide",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: '{"Title":"fghfdhgfdgfd"}',
        dataType: "json",

An this is my controler:

public class SlideController : ApiController
{

    // POST /api/Slide
    public void Post(string Title)
    {
    }

When I run the code and call the /api/Slide, the [Title] has no data and is null.

How do I post JSON to the API controller?

POST http://127.0.0.2:81/api/slide HTTP/1.1
Host: 127.0.0.2:81
Connection: keep-alive
Content-Length: 18
Origin: http://127.0.0.2:81
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://127.0.0.2:81/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Title=fghfdhgfdgfd
解决方案

Define a view model:

public class SlideViewModel
{
    public string Title { get; set; }
}

then have your controller action take this view model as argument:

public class SlideController : ApiController
{
    // POST /api/Slide
    public void Post(SlideViewModel model)
    {
        ...
    }
}

finally invoke the action:

$.ajax({
    type: 'POST',
    url: '/api/slide',
    cache: false,
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ title: "fghfdhgfdgfd" }),
    success: function() {
        ...
    }
});

The reason for that is that simple types such as strings are bound from the URI. I also invite you to read the following article about model binding in the Web API.

这篇关于POST JSON使用MVC 4 API控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:31