C# 将模型从.Net客户端发送到WebApi服务器
有一个很简单的问题,但我找不到答案。我有一个多字段的模型。例如:C# 将模型从.Net客户端发送到WebApi服务器,c#,asp.net-web-api,C#,Asp.net Web Api,有一个很简单的问题,但我找不到答案。我有一个多字段的模型。例如: public class BillingInfoViewModel { //Distributor Information public string DistributorName { get; set; } public string DistributorEmail { get; set; } public string DistibutorPhone { get; set; } pu
public class BillingInfoViewModel
{
//Distributor Information
public string DistributorName { get; set; }
public string DistributorEmail { get; set; }
public string DistibutorPhone { get; set; }
public CompanyCode CompanyCode { get; set; }
//ECU information
public string EcuName { get; set; }
public string EcuPartNumber { get; set; }
public string EcuProgramName { get; set; }
//Lot of fields
}
public class BillingController{
public IHttpActionResult Post([FromBody]BillingInfoViewModel model){
// HERE YOU GET THE BillingInfoViewModel obj from the call (in POST)
}
}
这是我应该发送给服务器的信息。我怎么能做到?我应该一个接一个地发送这些字段吗
public void ApiController(string DistributorName, string DistributorEmail, ..othern fields)
在什么情况下我可以发送所有对象
public void ApiController(BillingInfoViewModel model)
注意:如果您使用的是Asp.Net WebAPI,我将通过System.Net.Http.HttpClient发送信息 请尝试以下示例:
public class BillingInfoViewModel
{
//Distributor Information
public string DistributorName { get; set; }
public string DistributorEmail { get; set; }
public string DistibutorPhone { get; set; }
public CompanyCode CompanyCode { get; set; }
//ECU information
public string EcuName { get; set; }
public string EcuPartNumber { get; set; }
public string EcuProgramName { get; set; }
//Lot of fields
}
public class BillingController{
public IHttpActionResult Post([FromBody]BillingInfoViewModel model){
// HERE YOU GET THE BillingInfoViewModel obj from the call (in POST)
}
}
如果您正在使用Asp.NET WebAPI,希望它对您有所帮助 请尝试以下示例:
public class BillingInfoViewModel
{
//Distributor Information
public string DistributorName { get; set; }
public string DistributorEmail { get; set; }
public string DistibutorPhone { get; set; }
public CompanyCode CompanyCode { get; set; }
//ECU information
public string EcuName { get; set; }
public string EcuPartNumber { get; set; }
public string EcuProgramName { get; set; }
//Lot of fields
}
public class BillingController{
public IHttpActionResult Post([FromBody]BillingInfoViewModel model){
// HERE YOU GET THE BillingInfoViewModel obj from the call (in POST)
}
}
希望对您有所帮助正常情况下,复杂参数类型是通过POST动词传递的。您可以使用GET谓词,但它在浏览器中有url限制,并且参数可见。下面是参数绑定的详细指南,介绍了几种实现需求的方法
通常,复杂参数类型通过POST-verb传递。您可以使用GET谓词,但它在浏览器中有url限制,并且参数可见。下面是参数绑定的详细指南,介绍了几种实现需求的方法
从客户端到服务器,将此数据作为JSON发送。您的JSON属性名称应与模型类BillingInfo ViewModel的属性名称相同。您可以通过序列化为JSON对象来发送整个模型。从客户端到服务器,将此数据作为JSON发送。您的JSON属性名称应该与模型类BillingInfoViewModel的属性名称相同。您可以通过序列化为JSON对象来发送整个模型。