InvalidOperationException: Incorrect Content-Type


Question: How do you solve this error in Asp.net Core MVC Form Submission? The error says "An unhandled exception occurred while processing the requestInvalidOperationException: Incorrect Content-Type:

Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()"


Login to See the Rest of the Answer

Answer:

There is a chance that you have multiple forms on the same MVC View, if this is the case regardless of whether you add Content-Type or not, this will remain the issue.

One way to solve this error is by coding the Server-Side funcation as below:

        [Authorize]
        [HttpGet]
        [Route("SaveSomeData/{Id}/{Active}", Order = 0)]
        [Route("SaveSomeData", Order = 1)]
        public async Task<IActionResult> SaveSomeData(int Id,bool Active)
        {
            try
            {

                List<MyModal> adjacency = await _context.GeneratedAdjucency.Where(a => a.Id == Id && a.SomeProperty == 
                MyModal.Id).ToListAsync();

                if (adjucency is not null && adjucency.Count() > 0)
                {
                    adjucency.FirstOrDefault().IsActive = Active;
                    await _context.SaveChangesAsync();
                }
                
                return RedirectToAction("Preview", new { id = Id, refresh = true });
            }
            catch (Exception ex)
            {
             //ToDo
            }  
            return RedirectToAction("Preview", new { id = 0 });
        }


Code the MVC Form like this:

<form asp-action="SaveSomeData" method="get" 
  class="d-flex flex-row justify-content-between" enctype="multipart/form-data">
<div class="form-check form-switch m-2">
<label class="form-check-label m-auto" for="Active">Active</label>
<input class="form-check-input mt-2" onchange="form.submit()" type="checkbox" id="Active" checked>
<input hidden name="Id" value="@MyModal.Id" />
</div>
</form>


This Form above will produce some CheckBox like below:
Invalid Form Content-Type

In addition, The "InvalidOperationException: Incorrect Content-Type" error usually occurs in ASP.NET Core MVC when you are trying to read form data from an HTTP request using ReadForm() method of FormFeature class, but the request content type is not compatible with the method.

To resolve this error, you can follow the steps below:

  1. Ensure that you are using the correct content type for the request. ReadForm() method in ASP.NET Core MVC expects the request content type to be "application/x-www-form-urlencoded" or "multipart/form-data". Make sure that the client-side code or any middleware that processes the request sets the correct content type.

  2. If you are using "multipart/form-data" content type, ensure that the form enctype attribute is set to "multipart/form-data" in your HTML form. For example:

    <form enctype="multipart/form-data" action="/controller/action" method="post">
       <!-- form fields go here -->
       <input type="submit" value="Submit" />
    </form>
    
    1. If you are using "application/x-www-form-urlencoded" content type, make sure that the form data is properly encoded in the request body. For example, if you are using JavaScript/jQuery to submit the form, you can use the serialize() or serializeArray() methods to properly encode the form data.

    2. Check if there are any middleware or filters in your ASP.NET Core MVC pipeline that may be modifying the request content type. Make sure that any middleware or filters that process the request are not modifying the content type in a way that is incompatible with ReadForm() method.

    3. If you are still facing the issue, you can try using other methods to read the form data from the request, such as Request.Form or await Request.ReadFormAsync(), depending on your requirements.

    If you have checked all the above steps and are still facing the issue, you may need to further investigate your code and configuration to identify the root cause of the error.









For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]