HttpContext.Request.Form threw an exception of type 'System.InvalidOperationException' - why?

HttpContext.Request.Form threw an exception of type 'System.InvalidOperationException' - why?

cdvolkocdvolko Posts: 5Questions: 1Answers: 0

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.

Answers

  • cdvolkocdvolko Posts: 5Questions: 1Answers: 0

    An observation: In RegisterUser HttpContext.Request.HasFormContentType = false. In EditUser it is true.

  • cdvolkocdvolko Posts: 5Questions: 1Answers: 0
    // 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.

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Is this using Editor? Otherwise, it doesn't sound like a DataTables issue, so would be worth asking on StackOverflow or a site specific to .NET.

    Colin

  • cdvolkocdvolko Posts: 5Questions: 1Answers: 0

    Yes, it is about Editor. As I have already stated it is solved now.

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    I was querying as our accounts aren't showing that you have a license - is the license registered to another email address? Please can let us know so we can update our records.

    Colin

  • cdvolkocdvolko Posts: 5Questions: 1Answers: 0

    Our company has a license. I am registered with my private email address.

  • allanallan Posts: 63,237Questions: 1Answers: 10,418 Site admin

    Many thanks for the update. Great to hear you've got it resolved now :)

    Allan

Sign In or Register to comment.