有没有办法将字符串从 angular 7 传递到 .Net Core 2.0 API?
Is there a way to pass on a string from angular 7 to a .Net Core 2.0 API?
我正在尝试将字符串从我的 angular 7 服务传递到 .net API... 但它总是提供错误的内容。谁能建议我任何解决方案?
ANGULAR:
searchAgentCode(agent_code:string): Observable<prsProducer[]>{
debugger;
let url: string=this._global.serviceUrl+'Producers/checkAgentCode';
return this._httpClient.post<prsProducer[]>(url,agent_code);
}
API:
[HttpPost]
[Produces(typeof(List<ProducerDC>))]
[Route("checkAgentCode")]
public IActionResult checkAgentCode([FromBody]String obj_ProducerDC)
{
sys_ACTIVITY_LOG_Insert(Request);
try
{
ProducerBL obj_ProducerBL = new ProducerBL();
List<ProducerDC> producer = new List<ProducerDC>();
//List<sys_USERDC> list = new List<sys_USERDC>();
//list.Add(sys_USER);
int UpdatedCount = 0;
string agent_code = obj_ProducerDC.AGENT_CODE;
producer = obj_ProducerBL.checkAgentCode(agent_code);
return Ok(producer);
}
catch (Exception ex)
{
LogException(ex);
throw new Exception(ex.Message);
}
}
我们可以使用Class对象来存储参数。然后发送到 API.
searchAgentCode(agent_code:string): Observable<prsProducer[]>{
debugger;
Myclass myobj=new Myclass();
myobj.agent_code=agent_code;
let url: string=this._global.serviceUrl+'Producers/checkAgentCode';
return this._httpClient.post<prsProducer[]>(url,myobj);
}
\
对于API,制作具有相同属性的相同class,并在class对象中接收参数为:
[HttpPost]
[Produces(typeof(List<ProducerDC>))]
[Route("checkAgentCode")]
public IActionResult checkAgentCode([FromBody]MyClassforAPI obj_ProducerDC)
{
sys_ACTIVITY_LOG_Insert(Request);
try
{
string agent_code= obj_ProducerDC.agent_code;
//YOUR CODE
}
catch (Exception ex)
{
LogException(ex);
throw new Exception(ex.Message);
}
}
实际上,更好的做法是创建一个视图模型,例如 所示。
如果传递单个字符串是对您的严格要求,请查看 答案。
根据项目采用的 REST 方法,将其作为路由参数传递可能也是一种解决方案
[Route("checkAgentCode/{obj_ProducerDC}")]
public IActionResult checkAgentCode(string obj_ProducerDC)
{
// Logic here...
我正在尝试将字符串从我的 angular 7 服务传递到 .net API... 但它总是提供错误的内容。谁能建议我任何解决方案?
ANGULAR:
searchAgentCode(agent_code:string): Observable<prsProducer[]>{
debugger;
let url: string=this._global.serviceUrl+'Producers/checkAgentCode';
return this._httpClient.post<prsProducer[]>(url,agent_code);
}
API:
[HttpPost]
[Produces(typeof(List<ProducerDC>))]
[Route("checkAgentCode")]
public IActionResult checkAgentCode([FromBody]String obj_ProducerDC)
{
sys_ACTIVITY_LOG_Insert(Request);
try
{
ProducerBL obj_ProducerBL = new ProducerBL();
List<ProducerDC> producer = new List<ProducerDC>();
//List<sys_USERDC> list = new List<sys_USERDC>();
//list.Add(sys_USER);
int UpdatedCount = 0;
string agent_code = obj_ProducerDC.AGENT_CODE;
producer = obj_ProducerBL.checkAgentCode(agent_code);
return Ok(producer);
}
catch (Exception ex)
{
LogException(ex);
throw new Exception(ex.Message);
}
}
我们可以使用Class对象来存储参数。然后发送到 API.
searchAgentCode(agent_code:string): Observable<prsProducer[]>{
debugger;
Myclass myobj=new Myclass();
myobj.agent_code=agent_code;
let url: string=this._global.serviceUrl+'Producers/checkAgentCode';
return this._httpClient.post<prsProducer[]>(url,myobj);
}
\
对于API,制作具有相同属性的相同class,并在class对象中接收参数为:
[HttpPost]
[Produces(typeof(List<ProducerDC>))]
[Route("checkAgentCode")]
public IActionResult checkAgentCode([FromBody]MyClassforAPI obj_ProducerDC)
{
sys_ACTIVITY_LOG_Insert(Request);
try
{
string agent_code= obj_ProducerDC.agent_code;
//YOUR CODE
}
catch (Exception ex)
{
LogException(ex);
throw new Exception(ex.Message);
}
}
实际上,更好的做法是创建一个视图模型,例如
如果传递单个字符串是对您的严格要求,请查看
根据项目采用的 REST 方法,将其作为路由参数传递可能也是一种解决方案
[Route("checkAgentCode/{obj_ProducerDC}")]
public IActionResult checkAgentCode(string obj_ProducerDC)
{
// Logic here...