控制器方法中的HTTP POST请求崩溃.net核心1.1无异常或消息

时间:2018-01-25 11:47:28

标签: c# .net api http .net-core

我正在尝试向控制器中的服务器发出HTTP POST请求。由于某种原因,应用程序停止。没有例外或其他消息。

我正在使用RestEase来处理HTTP请求。我也尝试过使用HttpClient,但这会得到完全相同的结果:应用程序崩溃而没有消息。

控制器代码

 [Route("/api/[controller]")]
  [ApiValidationFilter]
  public class LeidingController : Controller
  {
    private readonly ILeidingRepository _leidingRepository;
    private readonly ITakRepository _takRepository;
    private readonly IMapper _mapper;
    private readonly Auth0Helper _auth0Helper;
    private readonly IConfiguration _configuration;

    public LeidingController(ILeidingRepository leidingRepository, ITakRepository takRepository, IMapper mapper, Auth0Helper auth0Helper, IConfiguration configuration)
    {
      _leidingRepository = leidingRepository;
      _takRepository = takRepository;
      _mapper = mapper;
      this._auth0Helper = auth0Helper;
      _configuration = configuration;
    }

    //.. Some more methods that aren't relevant

    [Route("{leidingId}/createUser")]
    [HttpGet]
    public async Task<IActionResult> CreateUser([FromRoute] int leidingId)
    {
      var leiding = _leidingRepository.FindById(leidingId);

      //Database operations
      if (leiding == null)
      {
        return NotFound(new ApiResponse(404, $"Opgegeven leiding met id {leidingId} werd niet gevonden"));
      }

      if (leiding.Email == null)
      {
        ModelState.AddModelError("NoEmail","De gebruiker heeft geen emailadres. Kan geen gebruiker maken zonder email");
        return BadRequest(new ApiBadRequestResponse(ModelState));
      }

      //Getting some config. values from appsettings.json
      var url = _configuration["Auth0:domain"];
      var clientId = _configuration["Auth0:ni_client_id"];
      var clientSecret = _configuration["Auth0:ni_client_secret"];
      var audience = _configuration["Auth0:audience"];

      //create parameters
      var keyValues = new Dictionary<string, string>
      {
        {"grant_type", "client_credentials"},
        {"client_id", clientId},
        {"client_secret", clientSecret},
        {"audience", audience}
      };

      IAuth0Api api = RestClient.For<IAuth0Api>(url);

      //After executing this line, the app crashes without any message.
      TokenViewModel result = await api.GetTokenAsync(keyValues);


      return Ok(result);
    }


  }

IAuth0Api

 public interface IAuth0Api
  {
    [Post("/oauth/token")]
    Task<TokenViewModel> GetTokenAsync([Body(BodySerializationMethod.UrlEncoded)] Dictionary<string,string> body);
  }

0 个答案:

没有答案