How does WebAPI receive parameters in vue and WebAPI

.NET Core
WebAPI The way of receiving the back-end when the post parameter is passed Since the front- end has used Vue's axios, the front-end interaction has become the request pyload mode by default, and the passed data has become the default.
The format of application/json data transfer is shown below Figure.
Since we have entered a new era, it is recommended to pass parameters in this way in the future.
Insert picture description here
Front-end code

axios.post('/api/FaHuoTongZhiDan/RemoveTZD',{
    
    "Id":Id} )
	    .then(res => {
    
    
	       res= res.data;
	       if (res.success)
	       {
    
    
	         this.$message.info(res.message);
	         this.fetch();
	       }else{
    
    
	         this.$message.error(res.message);
	       }
	
	     })
	     .catch(function (error) {
    
    
	       this.$message.error(error);
	     });

The way the background can receive parameters is as follows.

1. Entity class (recommended)
2. Dynamic dynamic type (recommended)
3. JObject parameter
4. Single value parameter (string parameter)

When there are many parameters, it is recommended to use the entity class to receive parameters, and
when there are few parameters, it is recommended to sample the dynamic dynamic class to receive parameters.

// 接收 pyload过来的数据必须是 [FromBody] 才能接收到, 
public AjaxRes RemoveTZD([FromBody] dynamic pars)
 {
    
    
     int Id = pars.Id;
     发货通知单 发货通知单 = DB.发货通知单s.Where(a=>a.Id == Id).FirstOrDefault();
      
     if (发货通知单 == null)
     {
    
    
         return new AjaxRes(false,"删除失败,找不到对应的商品记录.");
     }

     DB.发货通知单s.Remove(发货通知单);
     DB.SaveChanges(); 

     return new AjaxRes(true, "删除成功");
 }

Guess you like

Origin blog.csdn.net/phker/article/details/111242737