.NET Core WebAPI post parameters passed when the rear end of the receiving mode

Original: receiving a parameter passing mode when the rear end of the post .NET Core WebAPI

.NET Core WebAPI post parameters passed when the rear end of the receiving mode

  1. Entity class
  2. dynamic dynamic type
  3. JObject parameters
  4. Single parameter (string parameter)

Post request code distal A.

$.ajax({
           url: "/api/student/poststudent",
           type: 'POST',
           data:JSON.stringify({ name: "张三", age: 21 }),
           success:function () {
           },
           dataType: "json",
           contentType: "application/json"
       });

B. receiving the rear end of parametrically

1. entity class

Entity class is a simple way to pass parameters, very high frequency.

  1. Adding Entity Classes
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
  1. Background Processing Post request code
[HttpPost("{id}")]
public void PostStudent(Student student)
{
}
  1. Receive parameters results

2.dynamic dynamic type

  1. Background Processing Post request code
[HttpPost("{id}")]
public void PostStudent(dynamic student)
{
    var name = student.name;//name大小写与前端参数一致
    var age = student.age;
}
  1. Receive parameters results

3.JObject parameters

  1. Package introduced Microsoft.AspNetCore.Mvc.NewtonsoftJson
  2. Add Reference using Newtonsoft.Json.Linq;
  3. Background Processing Post request code
[HttpPost("{id}")]
public void PostStudent(JObject student)
{
}
  1. Receive parameters results

4. Single parameter (string parameter)

You could send a string parameter, and a double bag in single quotes and double quotation marks in a string content!

  1. Unlike the above embodiment the front end of the code
$.ajax({
           url: "/api/student/poststudent",
           type: 'POST',
           data:'"name=张三,age=21"',//这里是重点!用单引号包一个双引号,双引号里为字符串内容!
           success:function () {
           },
           dataType: "json",
           contentType: "application/json"
       });
  1. Background Processing Post request code
[HttpPost("{id}")]
public void PostStudent([FromBody] string values)
{
}

WebApi method parameters preceded [FromBody] identifier indicating the parameter value should be obtained from the Body request, instead of from a URL. Without [FromBody] Background fail to identify the parameter value.

  1. Receive parameters results

Settings allow cross-domain

If there is demand for cross-domain support, allowing cross-domain settings. Add the following code in Stateup.cs.

  1. ConfigureServices modification method, add the following code:
//允许一个或多个来源可以跨域
services.AddCors(options =>
{
      options.AddPolicy("CustomCorsPolicy", policy =>
      {
             // 设定允许跨域的来源,有多个可以用','隔开
             policy.WithOrigins("https://localhost:5000")//只允许https://localhost:5000来源允许跨域
             .AllowAnyHeader()
             .AllowAnyMethod()
             .AllowCredentials();
      });
});
  1. Configure modification method, add the following code:
app.UseCors("CustomCorsPolicy");

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12316554.html