MVC and WebApi get and post pass parameters. Reprinted https://blog.csdn.net/qq373591361/article/details/51508806

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq373591361/article/details/51508806
we summarize the mass participation method js requests to the server.

Get embodiment
Get mainly used to query, generally divided into no parameters, a parameter, a plurality of parameters, the parameter entity object.

1、无参
//Get没有参数
var get_f1 = function() {
$.ajax({
type: "get",
url: "/api/Demo",
success: function(data, status) {
if (status == "success") {
$("#div_test").html(data);
}
}
});
}
2、一个参数
//Get一个参数
var get_f2 = function() {
$.ajax({
type: "get",
url: "/api/Demo",
data: { strQuery: JSON.stringify({ Id: "1", Name: "Jim", CreateTime: "1988-09-11" }) },
contentType: "application/json",
success: function(data, status) {
if (status == "success") {
$("#div_test").html(data);
}
}
});
}
Background controller method
/// <Summary>
/// parameter
/// </ Summary>
/// <param name = "strQuery"> </ param>
/// <Returns> </ Returns>
[HttpGet]
the Get string public (string strQuery)
{
// a time parameter, if the distal end is passed over a string can json deserialized into objects.
Newtonsoft.Json.JsonConvert.DeserializeObject TbCharging oData = // <TbCharging> (strQuery);
// return the String.Format ( "{0}, {. 1}, {2}, {}. 3", oData.Id, oData. name, oData.Des, oData.CreateTime.ToString ( "the mM-dd-YYYY HH: mm: SS"));
return "parameter" + strQuery;
}

Note: Get in the way when we can put parameters on the url, I am here to write a unified front desk some, all data on the inside, feel look better.
3, a plurality of parameters
// Get a plurality of parameters
var get_f3 = function () {
$ .ajax ({
type: "GET",
URL: "/ API / Demo",
Data: {Id: ". 1", the Name: " jim ", CreateTime:" 1988-09-11 "},
Success: function (Data, Status) {
IF (Status ==" Success ") {
$ (" # div_test ") HTML (Data);.
}
}
}) ;
}
background controller method
/// <Summary>
/// plurality of parameters
/// </ Summary>
/// <param name = "Id"> </ param>
/// <param name = "the name" > </ param>
/// <param name = "CreateTime"> </ param>
/// <


{
return String.Format("多个参数,{0},{1},{2}", Id, Name, CreateTime.ToString("yyyy-MM-dd hh:mm:ss"));
}

4, a solid object parameter
// Get physical object as a parameter
var get_f4 = function () {
$ .ajax ({
type: "GET",
URL: "/ API / Demo / getModel",
Data: {Id: ". 1" , the Name: "Jim", CreateTime: "1988-09-11"},
Success: function (Data, Status) {
IF (Status == "Success") {
$ ( "# div_test") HTML (Data);.
}
}
});
}

Note: There is no discovery request to a plurality of parameters and a method of physical objects is the same, if the two are written in a method of receiving the same controller inside, it does not distinguish route is to match the processing method,
so to custom route used to distinguish, in the name of the method to change WebApi which is of no use.

Background controller method
/// <Summary>
/// an entity object parameter
/// </ Summary>
/// <param name = "oData"> </ param>
/// <Returns> </ Returns>
[ HttpGet]
[the Route ( "getModel")]
public String the Get ([FromUri] TbCharging oData)
{
return the String.Format ( "an entity object parameter, {0}, {1} , {2}, {3}", oData .id, oData.Name, oData.Des, oData.CreateTime.ToString ( "the mM-dd-YYYY HH: mm: SS"));
}

Note: When using the Get object parameter passing entity to pay attention, because the time parameter of the Get method is written in the url, so we use the background [FromBody] can not get physical parameters need to write [FromUri] job.
Post manner
Post general way we used to do add, delete, change operation. In WebApi the Post only be used to make an increase, modify with Put, delete with Delete. These new templates automatically generate time for us.

Also to mention that the Post can only pass a parameter, if required, when multiple parameters we need to combine them into an extension object to pass as objects.

When we received the background needed to write on the list of arguments inside [FromBody] because the parameters are not in the Post Url way inside.

1, a parameter
// Post a parameter
var post_f1 = function () {
$ .ajax ({
type: "POST",
URL: "/ API / Demo",
Data: { "": "Jim"},
// Data : "= Jim", // two ways for writing, other writing not obtain value
Success: function (the Data, Status) {
IF (Status == "Success") {
$ ( "# div_test") HTML (the Data);.
}
}
});
}

Note: When writing the parameters of the argument that we can not write data: { "name": "Jim"} In this way, because in the background is get value.

Background controller method
/// <Summary>
/// parameter
/// </ Summary>
/// <param name = "name"> </ param>
/// <Returns> </ Returns>
public String Post ([FromBody] String name)
{
return "parameter," + name;
}
2, a solid object parameter
// Post an object (Post not submit multiple parameters, parameters can be packaged into a plurality of objects)
var post_f2 function = () {
var PostData = {Id: ". 1", the Name: "Jim", CreateTime: "1988-09-11"};
$ .ajax ({
type: "POST",
URL: "/ API / Demo / PostAdd ", // plurality post when the route will not match, so custom routing
// data: {Id:" 1 ", Name:" Jim ", CreateTime:" 1988-09-11 "} ,
Data: PostData,
Success: function (Data,status) {
if (status == "success") {
$("#div_test").html(data);
}
}
});
}
Background controller method
/// <Summary>
/// object
/// </ Summary>
/// <param name = "oData"> </ param>
/// <Returns> </ Returns>
[HttpPost]
[the Route ( "PostAdd")]
public Object Post ([FromBody] TbCharging oData)
{
var = strName the String.Format ( "an object, {0}, {1} ", oData.Id, oData. name);
return strName;
}
Note: this parameter list is preferably written on [FromBody], while the write can not get value.
3, an array parameter
// Post array as a parameter
var post_f3 = function () {
var ARR = [ ". 1", "2", "3", ". 4"];
$ .ajax ({
type: "POST",
url: "/ api / Demo / PostArray",


Success: function (the Data, Status) {
IF (Status == "Success") {
. $ ( "# div_test") HTML (the Data);
}
}
});
}
Here we talk about contentType and dataType.
contentType

When the server transmits content information to the encoding type. If you do not write, default is: "application / x-www-form-urlencoded".

dataType

Expected data type returned by the server.

If we Post json parameter is submitted, it is best to write contentType: 'application / json'

Background of the controller method
/// <Summary>
/// array as a parameter
/// </ Summary>
/// <param name = "IDS"> </ param>
/// <Returns> </ Returns>
[ HttpPost]
[the Route ( "PostArray")]
public Object Post (String [] IDS)
{
return the String.Format ( "{0}, {. 1}, {2}", IDS [0], IDS [. 1], IDS [2]);
}
4, the set of physical parameters
// Post collection of objects, a plurality of identical objects (when a plurality of different objects can be packaged objects into a plurality of extended objects)
var post_f4 = function () {
var ARR = [
Id {: ". 1", the Name: "Jim", CreateTime: "1988-09-11"},
{Id: "2", the Name: "Lilei", CreateTime: "1990-12-11"},
{Id : "3", Name: " Lucy", CreateTime: "1986-01-10"}
];
$.ajax({
type: "post",
url: "/ api / Demo / PostMulti", // plurality post when the route will not match, so custom routing
contentType: 'file application / JSON',
Data: the JSON.stringify (ARR),
Success: function (Data, Status) {
IF (Status == "Success") {
$ ( "# div_test") HTML (Data);.
}
}
});
}
controller method background
/// <Summary>
/// Object set
/// </ Summary>
/// <param name = "lstCharging"> </ param>
/// <Returns> </ Returns>
[HttpPost]
[the Route ( "PostMulti")]
public Object Post ([FromBody ] List <TbCharging> lstCharging)
{
return the String.Format ( "{0}, {}. 1", lstCharging [0] .Name, lstCharging [. 1] .Name);
}
Custom routing
be used: a method overloading, ignores the method name, url customize

Steps for usage:

1, was added over a controller class marker
[RoutePrefix ( "API / Demo")]
public class DemoController: ApiController
{}

2, the routing tag adding process
/// <Summary>
/// array as a parameter
/// </ Summary>
/// <param name = "IDS"> </ param>
/// <Returns> </ Returns>
[HttpPost]
[the Route ( "PostArray")]
public Object Post ([FromBody] String [] IDS)
{
return the String.Format ( "{0}, {. 1}, {2}", IDS [0], IDS [. 1], IDS [2]);
}

NOTE: The above api / Demo and PostArray can define your own name. Access to "/ api / Demo / PostArray": the people like me can write directly url.
---------------------
Author: qq373591361
Source: CSDN
Original: https: //blog.csdn.net/qq373591361/article/details/51508806
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/Jeely/p/10958851.html