You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core双表单提交触发InvalidOperationException(Content-Type错误)求助

ASP.NET Core: InvalidOperationException (Incorrect Content-Type) with Combined Login/Register Form

Problem Description

I'm working on an ASP.NET Core project where I've placed both login and registration forms in the same view, using a single shared model to handle both sets of fields. When I try to access the login page, I immediately get an InvalidOperationException: Incorrect Content-Type error.

I've gone through relevant documentation and community resources but haven't found a solution that directly matches my scenario. Since I'm relatively new to C#/.NET Core, I'm struggling to pinpoint the root cause of this issue. I'm hoping to get some guidance here, and I've included my model, view, and controller code below for reference.

Relevant Code

1. Shared Model (AccountViewModel.cs)

public class AccountViewModel
{
    // Login fields
    [Required]
    [EmailAddress]
    public string LoginEmail { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string LoginPassword { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }

    // Registration fields
    [Required]
    [EmailAddress]
    public string RegisterEmail { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    public string RegisterPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("RegisterPassword", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

2. View (Login.cshtml)

@model AccountViewModel

@{
    ViewData["Title"] = "Login & Register";
}

<h1>@ViewData["Title"]</h1>

<!-- Login Form -->
<div class="row">
    <div class="col-md-6">
        <h2>Login</h2>
        <form asp-action="Login" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="LoginEmail" class="control-label"></label>
                <input asp-for="LoginEmail" class="form-control" />
                <span asp-validation-for="LoginEmail" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LoginPassword" class="control-label"></label>
                <input asp-for="LoginPassword" class="form-control" />
                <span asp-validation-for="LoginPassword" class="text-danger"></span>
            </div>
            <div class="form-group">
                <div class="checkbox">
                    <label asp-for="RememberMe">
                        <input asp-for="RememberMe" /> @Html.DisplayNameFor(model => model.RememberMe)
                    </label>
                </div>
            </div>
            <div class="form-group">
                <input type="submit" value="Login" class="btn btn-primary" />
            </div>
        </form>
    </div>

    <!-- Registration Form -->
    <div class="col-md-6">
        <h2>Register</h2>
        <form asp-action="Register" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="RegisterEmail" class="control-label"></label>
                <input asp-for="RegisterEmail" class="form-control" />
                <span asp-validation-for="RegisterEmail" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="RegisterPassword" class="control-label"></label>
                <input asp-for="RegisterPassword" class="form-control" />
                <span asp-validation-for="RegisterPassword" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ConfirmPassword" class="control-label"></label>
                <input asp-for="ConfirmPassword" class="form-control" />
                <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Register" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

3. Controller (AccountController.cs)

public class AccountController : Controller
{
    // GET: Account/Login
    public IActionResult Login()
    {
        return View();
    }

    // POST: Account/Login
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(AccountViewModel model)
    {
        if (ModelState.IsValid)
        {
            // Login logic here
            return RedirectToAction(nameof(HomeController.Index), "Home");
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

    // GET: Account/Register
    public IActionResult Register()
    {
        return View("Login"); // Reuse the same view for registration
    }

    // POST: Account/Register
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Register(AccountViewModel model)
    {
        if (ModelState.IsValid)
        {
            // Registration logic here
            return RedirectToAction(nameof(HomeController.Index), "Home");
        }

        // If we got this far, something failed, redisplay form
        return View("Login", model);
    }
}

Error Details

The error triggers immediately when navigating to /Account/Login (before submitting either form):

InvalidOperationException: Incorrect Content-Type

What I've Tried

  • Double-checked that both forms have method="post" set correctly
  • Verified ValidateAntiForgeryToken attributes are applied to all POST actions
  • Confirmed model properties have appropriate data annotations
  • Ensured the view correctly references the AccountViewModel

I'm confused why this Content-Type error is firing on page load rather than during form submission. Any insights or troubleshooting steps would be a huge help!

内容的提问来源于stack exchange,提问作者Theo Crowley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:21:39