2016-05-23 28 views
1

ASP.Net Core 1.0でいくつかのパラメータを使用してリクエストを送信したいと思います(問題があるかどうかわかりません)。ASP.Net MVCはajax呼び出しのパラメータをマップしません

私は次のモデルがあります:

function sendRegistration(nodeName, nodeType) { 
    var model = { 
     ServiceType: $("#serviceType").html(), 
     BatchTimeout: $("#batchTimeout").html(), 
     BatchSize: $("#batchSize").html(), 
     FinishingType: $("#finishingType").html() 
    }; 

    var request = { 
     nodeName: nodeName, 
     nodeType: nodeType, 
     model: model 
    } 

    //$.post('@Url.Action("SendRegistration")', request); 
    $.ajax({ 
     url: '@Url.Action("SendRegistration")', 
     type: "POST", 
     dataType: 'json', 
     data: JSON.stringify(request), 
     async: false, 
     cache: false, 
     traditional: true, 
     contentType: 'application/json' 
    }); 
} 

しかし、サーバー上:私もそれを呼び出そうとJSを持っている

public void SendRegistration(string nodeName, NodeTypeEnum nodeType, RegistrationConfigContract model) 
{ 

} 

:私は、コントローラのメソッド次いる

[DataContract] 
public class RegistrationConfigContract 
{ 
    [DataMember] 
    public string ServiceType { get; set; } 
    [DataMember] 
    public int BatchTimeout { get; set; } 
    [DataMember] 
    public int BatchSize { get; set; } 
    [DataMember] 
    public ActorFinishingTypeEnum FinishingType { get; set; } 
} 

を私はいつもnullを取得しますが、JS側ではすべてがうまくいますが、Chrome/Edgeデバッガによると、

私はここで間違っていますか?私はこの問題のためにgoogledコードを使用しようとしましたが、動作しません。

生のHTTPリクエスト:

POST http://localhost:8080/Configuration/SendRegistration HTTP/1.1 
Host: localhost:8080 
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0 
Accept: application/json, text/javascript; q=0.01 
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3 
Accept-Encoding: gzip, deflate 
DNT: 1 
Content-Type: application/json 
X-Requested-With: XMLHttpRequest 
Referer: http://localhost:8080/Configuration 
Content-Length: 156 
X-Compress: 1 
Proxy-Authorization: 6e9b34bd44817cf5c254e1ccb4fe7b31ecd526ea9e025e06a16baca626af6be0ea7fa102c2205f0e 
Connection: keep-alive 

{"nodeName":"zhdp","nodeType":"DocumentProducer","model":{"ServiceType":"jjjjjj","BatchTimeout":"33535","BatchSize":"6666","FinishingType":"UpdateMessage"}} 

答えて

1

問題はモデルバインドで... ASP.NETコアのモデルバインドが異なります。 アクションのパラメータに [FromBody]や[FromForm]などの適切な属性を設定する必要があります。また、あなたが通常のフォームポストやAjaxの ポストを持つことができるのいずれかが、ここでは両方ではない...

さらに詳しい情報:https://andrewlock.net/model-binding-json-posts-in-asp-net-core/

+0

私は答え自分自身を見つけましたが、とにかくリンクに感謝、それはいくつかの興味深い持っていますinfo in。 –

0

ASP.Net Coreバグ(または機能)だと思われます。私は両方のクエリ文字列パラメータとモデルを混在させました。私はそれをした後、それはクエリ文字列をバインドできますが、要求本体をバインドすることはできません。 [FromBody]属性を追加した後、すべてがうまく機能します。

JS:fstring.Format類似体である

function sendRegistration(nodeName, nodeType) { 
    var model = { 
     ServiceType: $("#serviceType").html(), 
     BatchTimeout: $("#batchTimeout").html(), 
     BatchSize: $("#batchSize").html(), 
     FinishingType: $("#finishingType").html() 
    }; 

    $.ajax({ 
     url: '@Url.Action("SendRegistration")?nodeName={0}&nodeType={1}'.f(nodeName, nodeType), 
     type: "POST", 
     dataType: 'json', 
     data: JSON.stringify(model), 
     cache: false, 
     traditional: true, 
     contentType: 'application/json' 
    }); 
} 

。 C#側:

[HttpPost] 
public void SendRegistration(string nodeName, NodeTypeEnum nodeType, [FromBody] RegistrationConfigContract model) 
{ 

} 

[HttpPost]はここでは必要ありませんが、それはその不在よりも私の意図が良く示しています。

関連する問題