【问题标题】:How map C# class to angular object如何将 C# 类映射到角度对象
【发布时间】:2020-02-07 14:39:06
【问题描述】:

我有一个对象类,我想在我的 ASP.NET Core Angular 项目中使用它。我无法通过 http get 方法映射对象返回。请问有什么选择吗?

类文件:

[Serializable]
public class PricesRules
{
    public HashSet<Price> Prices { get; set; }
    public HashSet<Customer> Customers { get; set; }
    public HashSet<Payment> Payments { get; set; }
}
component.ts :

public prices: PricesRules;

constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) {
  http.get<PricesRules>(baseUrl + 'api/UpdatePrices/GetLastPrices').subscribe(result => {
      this.prices = result[0];
}, error => console.error(error));
}

interface PricesRules {
Prices: any[];
Customers: any[];
Payments: any[];
}

控制器文件:

[HttpGet("[action]")]
public IEnumerable<PricesRules> GetLastPrices()
{
    PricesRules pricesRules = null;
    //some code here
    yield return pricesRules;
}

在我的组件中,我的结果对象中有很好的值,但之后我的对象价格未定义。

编辑:现在 get 方法没问题,但我的 post 方法没有触发我的控制器。

组件.ts '''

  onClickSubmit(data) {   

  const params = new HttpParams().set('ID', '1');
  const headers = new HttpHeaders().set('content-type', 'application/json');
  this.http.post<PricesRules>(this.baseUrl + 'api/UpdatePrices/PostUpdatePrices' + this.prices, { headers, params }).subscribe(result => {
      console.log("success");
  }, error => console.error(error));

} '''

控制器

'''

[HttpPost("[action]")]
    public async Task<IActionResult> PostUpdatePrices([FromBody] PricesRules pricesRules)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        return null;
   }

'''

我有这个错误:

Object { headers: {…}, status: 404, statusText: "Not Found", url: "https://localhost:44374/api/UpdatePrices/PostUpdatePrices[object%20Object]", ok: false, name: "HttpErrorResponse", message: "Http failure response for https://localhost:44374/api/UpdatePrices/PostUpdatePrices[object%20Object] : 404 Not Found", 错误: "\n\n\n\nError\n\n\n

无法发布 /api/UpdatePrices/PostUpdatePrices%5Bobject%20Object%5D
\n\n\n" }

【问题讨论】:

  • 您正在从服务器返回一个IEnumerable&lt;PricesRules&gt;,但在您的前端,您已经表明您的获取请求的返回值将只是PricesRules 的一个实例@ 即http.get&lt;PricesRules&gt;。这可能是问题的原因?
  • 是的,我已经修改了它,但它仍然是一样的

标签: c# angular asp.net-core mapping http-get


【解决方案1】:

我假设你的 api 返回 PricesRules 而不是 IEnumerable&lt;PricesRules&gt;(正如你所说的你得到数据)。所以因为结果将是一个像 {Prices:[...],...} 这样的对象,所以你不能通过索引访问它,你需要更改

this.prices = result[0];

this.prices = result.Prices;

this.prices = result['Prices'];

【讨论】:

  • 感谢您的快速回答!你对 api 返回是正确的。但是问题还是一样,我的本地对象在我的component.ts中不要复制结果的数据。
  • @jerome 你能显示console.log(result) 记录到控制台的内容吗
  • 我已经更改了我的 html 文件中的字段并显示了数据。在调试中,我的对象仍然未定义。谢谢。我现在在 post 方法中有一个问题,在我的控制器方法中,post 没有触发。我有这个错误:
【解决方案2】:

我已经修改了component.ts

'''

 const headers = new HttpHeaders()

      .set("Content-Type", "application/json");



  this.http.post(this.baseUrl + 'api/UpdatePrices/PostUpdatePrices',

      this.prices,

      { headers })

      .subscribe(

          val => {

              console.log("PUT call successful value returned in body",

                  val);

          },

          response => {

              console.log("PUT call in error", response);

          },

          () => {

              console.log("The PUT observable is now completed.");

          }

      );

'''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 2011-08-09
    • 1970-01-01
    • 2012-04-16
    • 2010-09-10
    • 2021-08-02
    • 1970-01-01
    • 2013-06-16
    相关资源
    最近更新 更多