有没有办法将字符串从angular 7传递到.Net Core 2.0 API?

时间:2019-07-01 05:57:30

标签: .net angular asp.net-core .net-core angular7

我正在尝试将我的angular 7服务中的字符串传递给.net API ...,但是它总是会给出错误的Content。谁能建议我任何解决方案?

英语:

#include <string>
#include <vector>
#include <tuple>
#include <iostream>

class geo
{
public:
    template<class...Args>
    geo(Args&&...);

private:
    auto as_tuple() const { 
        // note: mentioned in lexographical order
        return std::tie(_id, name, is_primary, position); 
    }

    friend auto operator ==(geo const& l, geo const& r) -> bool
    {
        return l.as_tuple() == r.as_tuple();
    }

    friend auto operator <(geo const& l, geo const& r) -> bool
    {
        return l.as_tuple() < r.as_tuple();
    }

private:
    int _id, is_primary;
    std::vector<int> position;
    std::string name;
};


int main()
{
    geo i= geo(2.1, 2.82, 1, 0, 0, 180, 0, "Patient-1",1);
    geo j= geo(2.1, 2.82, 1, 0, 0, 180, 0, "Patient-1",1);

    if(i==j) {
        std::cout<<"they are equal\n";
    }

    if(i < j) {
        std::cout<<"i is less\n";
    }
}

API:

searchAgentCode(agent_code:string): Observable<prsProducer[]>{
        debugger;
        let url: string=this._global.serviceUrl+'Producers/checkAgentCode';
        return this._httpClient.post<prsProducer[]>(url,agent_code);
      }

2 个答案:

答案 0 :(得分:1)

我们可以使用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,使用相同的属性制作相同的类,并在类对象中接收参数:

    [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);
                }
            }

答案 1 :(得分:0)

实际上,更可取的做法是创建一个视图模型,例如Hur Abbas所示。

如果严格要求传递单个字符串,请查看this答案。

根据项目所采用的REST方法,也可能是将其作为路由参数传递的解决方案

[Route("checkAgentCode/{obj_ProducerDC}")]
public IActionResult checkAgentCode(string obj_ProducerDC)
{
    // Logic here...