0

I stumbled upon a problem. I got an Angular 5 website and a backend based on C# WebApi.

In my webiste i got a component that is suppouse to send a post request to backend adding new entity to DataBase. Unfortunatelly it doesn't send a request or it isn's recieved on backend.

bet.component.ts

import {Injectable} from '@angular/core';
import {Service} from './service';
import {Http, Headers, RequestOptions, Response} from '@angular/http';
import {AppConfig} from '../app-config';
import {Bet} from '../models/bet';

@Injectable()
export class BetService extends Service {

  private url = this.config.apiUrl + '/bet';

  constructor(private http: Http, private config: AppConfig) {
    super();
  }

  addBet(bet: Bet) {
    return this.http.post(this.url + '/add', bet);
  }
}

BetController.cs

using System;
using System.Threading.Tasks;
using FakeBet.API.DTO;
using FakeBet.API.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FakeBet.API.Controllers
{
    [Route("api/[controller]")]
    public class BetController : Controller
    {
        private IBetService service;

        public BetController(IBetService service)
        {
            this.service = service;
        }

        [HttpPost("[action]")]
        public async Task<IActionResult> Add([FromBody] BetDTO bet) 
        {
            try
            {
                await service.AddBetAsync(bet);
                return Ok();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}

Url is ok.

I tried to change bet.component.ts to

return this.http.post(this.url + '/add', bet).subscribe(x => console.log('test'));

and then the response is recieved on the backend side, but BetDto bet is null. While bet is not null and the names of the properties are the same in the object in Angular and Api.

I also tried to send a request using a Postman and it went smooth. Here's how it looks. request

Can someone tell me where's my mistake? Cheers!

BetDTO

using System;
using FakeBet.API.Models;

namespace FakeBet.API.DTO
{
    public class BetDTO
    {
        public Guid BetId { get; set; }

        public string MatchId { get; set; }

        public string UserId { get; set; }

        public int BetOnTeamA { get; set; }

        public int BetOnTeamB { get; set; }
    }
}

bet.ts

export class Bet {
  matchId: string;
  userId: string;
  betOnTeamA: number;
  betOnTeamB: number;
}
9
  • Please post a minimal reproducible example not random parts of code Commented Mar 21, 2018 at 11:25
  • 1
    I don't see any HttpClient here. There's an Angular script that calls a Web API controller passing a JSON string in the body. If that body doesn't match the BetDTO class the parameter will be null. What does BetDTO look like? And btw the action method is named Add so the URL should be /add, not addBet Commented Mar 21, 2018 at 11:28
  • If POSTMAN worked, it means that the Angular and C# objects are not the same or that the requests were different. Use Fiddler to check how the two requests look and how they differ. Is there a missing content type header perhaps? Wrong encoding of the DTO? Commented Mar 21, 2018 at 11:29
  • is the content-type header being set in the angular part? Commented Mar 21, 2018 at 11:54
  • @LewisTaylor the headers aren't problem. It is set by default, i tried to set them manually and it still doesn't work. Commented Mar 21, 2018 at 13:14

1 Answer 1

1

Ok i found a solution, and its something I would never expect to be a thing. So when i was requesting a resource on webapi i was passing a request with a one field equals to "betOnTeamA" = "" or something like this. But apparently when a field is like this then .net think than whole body of a response is null. So a solution is to simply change "betOnTeamA" = "" to "betOnTeamA" = "0", and then it's working like a charm.

Sign up to request clarification or add additional context in comments.

2 Comments

Are you talking about BetOnTeamA ? If so, that makes sense, you can't convert "" to an int.
@DanDumitru yea i meant it fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.