![](/img/trans.png)
[英]how to fix getting { upload failed with 400 - Generic/unknown HTTP error } when trying to upload login form
[英]Getting HTTP ERROR 400 when form gets posted
我有一個 asp.net 核心剃須刀頁面,其中有一個簡單的表單,要求提供用戶電子郵件地址和提交按鈕。 當我輸入電子郵件並單擊提交按鈕時,我總是收到 400 錯誤
HTTP 錯誤 400
我不確定我在這里做錯了什么。 我嘗試在 OnPost 方法中放置一個斷點,但我什至沒有達到這一點。
下面是我的代碼:
家庭.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>
家庭.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
}
}
}
我發現了問題所在。 問題是它缺少表單中的防偽標記。
我只是添加了@Html.AntiForgeryToken();
在表單標簽內,現在一切都按預期工作。
家庭.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>
似乎當您擁有一個 asp.net core mvc 應用程序並且向其中添加一個 razor 頁面並嘗試創建一個表單時,它不會默認為 asp.net core 的Form Tag Helper 。
如果將此行添加到 Homie.cshtml 頁面, @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
它將自動使其成為表單標簽助手。 看這里。
所以我將代碼 Homie.cshtml 更改為:
家庭.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>
我有一個 asp.net core razor 頁面,其中有一個簡單的表單,要求提供用戶電子郵件地址和提交按鈕。 當我輸入電子郵件並單擊提交按鈕時,我總是收到 400 錯誤
HTTP 錯誤 400
我不確定我在這里做錯了什么。 我嘗試在 OnPost 方法中放置一個斷點,但我什至沒有達到那個點。
下面是我的代碼:
好人.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>
好人.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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.