Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

487
Visualizações
HttpContext.Request.Form threw an exception of type 'System.InvalidOperationException' - why?

I have a strange problem. We are using C# for an ASP.NET MVC application, and JavaScript with jQuery and Data Tables for the frontend. In the frontend, a part of the code looks like this:

        table: "#restapiusers",
        ajax: {
            create: { url: "/restapiuser/RegisterUser" },
            edit: { url: "/restapiuser/EditUser" }
        },

The thing is that although both endpoints RegisterUser and EditUser get passed the payload in the same manner, only EditUser works.

        [HttpPost]
        public async Task<JsonResult> RegisterUser(RestApi_UserModel user_short)
        {
            var param = HttpContext.Request.Form.Keys.FirstOrDefault();

When I set a breakpoint to the above line and move the mouse cursor over "Form", it says that HttpContext.Request.Form threw an exception of type 'System.InvalidOperationException'.

The beginning of EditUser looks exactly the same and there the error does not occur. I am puzzled how this is possible.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In one of the code files there was the following text:

// Defaults for DT-Editor for all future instances
$.extend(true, $.fn.dataTable.Editor.defaults, {
    ajax: {
        create: {
            type: 'POST',
            //*** contentType: "application/json",
            //*** dataType: 'json',
            data: function (d) {
                d = JSON.stringify(Object.values(d.data))
                console.info("Payload", d);
                return d
            },
        },
        edit: {
            type: 'POST',
            //contentType: "application/json",
            //dataType: 'json',
            data: function (d) {
                d = JSON.stringify(Object.values(d.data))
                console.info("Payload", d);
                return d
            },
        }
    },
    idSrc: 'id',
});

I commented out the lines I marked with "***" and now it works. Apparently the contentType must not be "application/json" for the data to be passed via the first or default key of the form.

about 4 years ago · Santiago Trujillo Relatório

0

Change Request.Form to Request.Query

public async Task<JsonResult> RegisterUser(RestApi_UserModel user_short)
 {
   var param = HttpContext.Request.Query.Keys.FirstOrDefault();
}```
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda