2013年9月11日星期三

ASP.NET MVC4 Web Api in HttpParameterBinding how to obtain the target object type

Action in such a way :

public bool InertMulit(List<Answer> answer)
{

}

Then I want to achieve such an effect , Request-Body as json format string , such as

[{"AID":123},{"AID":124}]

then the request is automatically bound to the List type of answer parameter

looked at did not find support for the system default , and then tried it and found that MVC and web api binding mechanisms are not the same , but not with the ModelBinder HttpParameterBinding.
My idea is to get to the request RequestBody the string, and then serialize the corresponding type of object, such as the above mentioned List .

now ask is , web api there are no available default implementation can achieve this effect?
If not, how the methods inherited from HttpParameterBinding

public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, 
            HttpActionContext actionContext, CancellationToken cancellationToken)

get the current parameters of the type it ? Serialization requires a target type.
------ Solution ---------------------------------------- ----
http://bbs.csdn.net/ topics/390530662? page = 1 # post-395162628
14th floor fangxinggood see answer
------ Solution ------------------------------ --------------
http://tech.pro/tutorial/1289/building-rest-api-with-mvc-4-web-api-part-2
- ---- For reference only ---------------------------------------
use reflection , available.

we have no experience or a better way of doing things ?
------ For reference only -------------------------------------- -

ah . Only saw 10 # ah
The Attribute more curious that FromBody
------ For reference only ---------------------------- -----------

with fiddler tried again, to understand. . The original so easy. .
expired before ModelBinder Only some aspects of the article , go the wrong direction the
------ For reference only ------------------ ---------------------

very good article , thank you recommended ! ~
------ For reference only ------------------------------------- -
then posts a
http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
--- --- For reference only ---------------------------------------
public bool InertMulit(IEnumerable<Answer> answer)
{
 
}

This trip was last seen forgot mvc source turned back today when suddenly think of it
------ For reference only ------- --------------------------------
 recently started to learn php a look at zend framework
------ For reference only ---------------------------------------

so without [ FromBody] also OK ?
------ For reference only -------------------------------------- -

so without [FromBody] also OK ?   possible
------ For reference only ---------------------------- -----------

so without [FromBody] also OK ?   You can try it yourself
------ For reference only -------------------------- -------------
using MvcApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MvcApplication.Controllers
{
    public class TestController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public string Get(int id)
        {
            return "value";
        }

        public void Post([FromBody]string value)
        {
        }

        public void Put(int id, [FromBody]string value)
        {
        }

        public void Delete(int id)
        {
        }


        [HttpPost]
        public IEnumerable<TestJsonRequest> TestListJson(IEnumerable<TestJsonRequest> model)
        {
            return model;
        }

        [HttpPost]
        public TestJsonRequest TestJson(TestJsonRequest model)
        {
            return model;
        }
    }
}

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script src="~/Scripts/json2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var jsonData = JSON.stringify([{ Id: 1, Name: "asasas" }, { Id: 1, Name: "2" }]);
            $.ajax({
                url: "http://localhost:33516/api/Test/TestListJson",
                type: "POST",
                dataType: "json",
                data: jsonData,
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    alert(data[0].Id);
                }

            });
        })

        $(function () {
            var jsonData = JSON.stringify({ Id: 1, Name: "2" });
            $.ajax({
                url: "http://localhost:33516/api/Test/TestJson",
                type: "POST",
                dataType: "json",
                data: jsonData,
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    alert(data.Id);
                }

            });
        })
    </script>
</head>
<body>
    <div>
    </div>
</body>
</html>

没有评论:

发表评论