Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

486
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!