Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting HTTP ERROR 400 when form gets posted

I have an asp.net core razor page where I have a simple form that asks for a user email address and a submit button. When I enter the email and click the submit button I'm always getting a 400 error

HTTP ERROR 400

I'm not sure what I'm doing wrong here. I tried putting a break point right inside the OnPost method, but I'm not even getting to that point.

Below is my code:

Homie.cshtml

@page
@model WebApplication1.Pages.HomieModel
@{
    ViewData["Title"] = "Homie";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Homie</h1>

<form method="post">
    <input type="email" name="emailaddress">
    <input type="submit">
</form>

Homie.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Pages
{
    public class HomieModel : PageModel
    {
        public void OnGet(string n)
        {
        }

        public void OnPost()
        {
            var emailAddress = Request.Form["emailaddress"];
            // do something with emailAddress
        }

    }
}

Error message (screen capture): enter image description here

like image 389
psj01 Avatar asked Apr 25 '19 01:04

psj01


People also ask

Why do I keep getting HTTP Error 400 the size of the request headers is too long?

The "Request header too large" message is thrown with an HTTP error code 400. This error occurs if the size of the request header has grown so large that it exceeds the maximum-allowed size. We recommend that you use the latest version of the SDK. Use at least version 3.

How do I fix HTTP Error 400 a request header field is too long?

This issues is usually caused by a corrupted cookie that is too long. Clear the Cache and remove the Cookies for websites that cause problems via the "3-bar" Firefox menu button (Options/Preferences). If clearing cookies didn't help then it is possible that the cookies.


1 Answers

I found out what the issue was. The problem was that it was missing the anti forgery token in the form.

I simply added @Html.AntiForgeryToken(); inside of the form tag and everything is working as expected now.

Homie.cshtml

@page
@model WebApplication1.Pages.HomieModel
@{
    ViewData["Title"] = "Homie";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Homie</h1>

<form method="post">
    <input type="email" name="emailaddress">
    <input type="submit">
    @Html.AntiForgeryToken();
</form>

It seems that when you have an asp.net core mvc application and when you add a razor page in to it and try to create a form it doesn't default to the Form Tag Helper for asp.net core.

If you add this line to the Homie.cshtml page, @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers it will automatically make it a form tag helper. See here.

So I changed my code Homie.cshtml to:

Homie.cshtml

@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model WebApplication1.Pages.HomieModel
@{
    ViewData["Title"] = "Homie";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Homie</h1>

<form method="post">
    <input type="email" name="emailaddress">
    <input type="submit">
</form>
like image 129
psj01 Avatar answered Sep 18 '22 08:09

psj01