This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
InvalidOperationException: Incorrect Content-Type: application/json #2749
Comments
Can you show your controller? |
I just realize that I put a ValidateAntiForgeryToken attribute on my method. using System;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
using Ict7Sponsoring.DTOs;
using Ict7Sponsoring.Filters;
using Ict7Sponsoring.Models;
using Microsoft.AspNet.Mvc;
namespace Ict7Sponsoring.Controllers
{
[Route("api/[controller]")]
public class RegistrationController : Controller
{
private readonly SubscriptionContext _context;
private readonly SendGridMail _sendGridMail;
public RegistrationController(SubscriptionContext context, SendGridMail sendGridMail)
{
_context = context;
_sendGridMail = sendGridMail;
}
[HttpPost("")]
[ValidateAntiForgeryToken]
[ValidateModelState]
public async Task<IActionResult> Register(SubscriptionDTO dto)
{
try
{
using (var tran = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
if (_context.Subscriptions.Any(x => x.Email == dto.Email))
{
ModelState.AddModelError("Email", "E-mail address already registered !!!");
return HttpBadRequest(ModelState);
}
var subscription = new Subscription
{
LastName = dto.LastName,
FirstName = dto.FirstName,
Email = dto.Email,
Function = dto.Function,
Company = dto.Company,
Phone = dto.Phone,
Club = dto.Club,
FederationNumber = dto.FederationNumber.GetValueOrDefault(),
Handicap = dto.Handicap.GetValueOrDefault(),
RegisterForTournament = dto.RegisterForTournament,
Comment = dto.Comment
};
_context.Subscriptions.Add(subscription);
await _context.SaveChangesAsync();
await _sendGridMail.SendMail(subscription);
tran.Complete();
return new HttpStatusCodeResult(200);
}
}
catch (Exception ex)
{
return HttpBadRequest(ex);
}
}
}
} and just in case, the validatemodelstate class: using Microsoft.AspNet.Mvc;
namespace Ict7Sponsoring.Filters
{
public class ValidateModelStateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid)
{
context.Result = new BadRequestObjectResult(context.ModelState);
}
}
}
} |
@davidfowl - agreed, we should make this fail more gracefully. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
When I POST to a controller using a json payload, I receive an InvalidOperationException: Incorrect Content-Type: application/json
Here is the raw request (captured by fiddler):
the headers of the response:
and finally the stacktrace:
The text was updated successfully, but these errors were encountered: