У меня есть класс объекта, и я хочу использовать его в своем угловом проекте ASP.NET Core. Я не могу отобразить объект возврата через 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 не запускает мой контроллер.
component.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 {заголовки: {…}, статус: 404, текст состояния: «Не найден», URL: «https://localhost:44374/api/UpdatePrices/PostUpdatePrices[object%20Object]", ok: false, имя:« HttpErrorResponse », сообщение:« Http-ошибка ответа для https://localhost:44374/api/UpdatePrices/PostUpdatePrices[object%20Object]: 404Не найдено ", ошибка:" \ n \ n \ n \ nОшибка \ n \ n \ n
Cannot POST /api/UpdatePrices/PostUpdatePrices%5Bobject%20Object%5D
\ n \ n \ n "}