无法在 Razor Pages 中调用 onPost
Unable to call onPost in Razor Pages
我想使用 POST 方法将 JSON 数据发送到剃刀页面:https://localhost:port/Post
并获得 JSON 输出。
例如:
我想将用户名和电子邮件传递为 JSON:
{"username":"test","email":"test@mail.com"}
到Post.cshtml页面的onPost方法:https://localhost:44363/Post
:
public JsonResult OnPost([FromForm] Dictionary<String, String> FormValues)
{
String username = FormValues["username"];
String email = FormValues["email"];
Dictionary<String,String> dict = new Dictionary<String, String>();
dict.Add("username", username);
dict.Add("email", email);
return new JsonResult(dict);
}
我在 WinForms 中使用 HttpClient 调用此 POST 方法:
HttpClient client = new HttpClient();
Dictionary<String, String> dict1 = new Dictionary<String, String>();
dict1.Add("username", "test");
dict1.Add("email", "test@mail.com");
String JSON = JsonConvert.SerializeObject(dict1);
HttpResponseMessage Result;
Result = client.PostAsync("https://localhost:44363/Post", new StringContent(JSON, Encoding.UTF8, "application/json")).Result;
String json = Result.Content.ReadAsStringAsync().Result;
Dictionary<String, String> dict2 = JsonConvert.DeserializeObject<Dictionary<String, String>>(json);
但我收到 bad request 400
错误。
我也试过 FromBody
作为:public JsonResult OnPost([FromBody] Dictionary<String, String> FormValues)
但是 onPost
没有执行。
但是,OnGet
方法运行良好:
public JsonResult OnGet()
{
Dictionary<String, String> dict = new Dictionary<String, String>();
dict.Add("username", "test");
dict.Add("email", "test@mail.com");
return new JsonResult(dict);
}
对于HttpClient
:
HttpClient client = new HttpClient();
HttpResponseMessage Result;
Result = client.GetAsync("https://localhost:44363/Post").Result;
String json = Result.Content.ReadAsStringAsync().Result;
Dictionary<String, String> dict2 = JsonConvert.DeserializeObject<Dictionary<String, String>>(json);
POST 个处理程序 Razor Pages have request verification enabled by default。这是为了防止跨站请求伪造。通常,建议是在请求中包含验证令牌,但在您的情况下,最简单的解决方案是在 PageModel 级别禁用对令牌的检查:
[IgnoreAntiforgeryToken(Order = 1001)]
public class PostModel : PageModel
{
...
顺便说一句,如果您要发布 JSON,您需要在处理程序参数上使用 FromBody
属性,而不是 FromForm
。
我想使用 POST 方法将 JSON 数据发送到剃刀页面:https://localhost:port/Post
并获得 JSON 输出。
例如:
我想将用户名和电子邮件传递为 JSON:
{"username":"test","email":"test@mail.com"}
到Post.cshtml页面的onPost方法:https://localhost:44363/Post
:
public JsonResult OnPost([FromForm] Dictionary<String, String> FormValues)
{
String username = FormValues["username"];
String email = FormValues["email"];
Dictionary<String,String> dict = new Dictionary<String, String>();
dict.Add("username", username);
dict.Add("email", email);
return new JsonResult(dict);
}
我在 WinForms 中使用 HttpClient 调用此 POST 方法:
HttpClient client = new HttpClient();
Dictionary<String, String> dict1 = new Dictionary<String, String>();
dict1.Add("username", "test");
dict1.Add("email", "test@mail.com");
String JSON = JsonConvert.SerializeObject(dict1);
HttpResponseMessage Result;
Result = client.PostAsync("https://localhost:44363/Post", new StringContent(JSON, Encoding.UTF8, "application/json")).Result;
String json = Result.Content.ReadAsStringAsync().Result;
Dictionary<String, String> dict2 = JsonConvert.DeserializeObject<Dictionary<String, String>>(json);
但我收到 bad request 400
错误。
我也试过 FromBody
作为:public JsonResult OnPost([FromBody] Dictionary<String, String> FormValues)
但是 onPost
没有执行。
但是,OnGet
方法运行良好:
public JsonResult OnGet()
{
Dictionary<String, String> dict = new Dictionary<String, String>();
dict.Add("username", "test");
dict.Add("email", "test@mail.com");
return new JsonResult(dict);
}
对于HttpClient
:
HttpClient client = new HttpClient();
HttpResponseMessage Result;
Result = client.GetAsync("https://localhost:44363/Post").Result;
String json = Result.Content.ReadAsStringAsync().Result;
Dictionary<String, String> dict2 = JsonConvert.DeserializeObject<Dictionary<String, String>>(json);
POST 个处理程序 Razor Pages have request verification enabled by default。这是为了防止跨站请求伪造。通常,建议是在请求中包含验证令牌,但在您的情况下,最简单的解决方案是在 PageModel 级别禁用对令牌的检查:
[IgnoreAntiforgeryToken(Order = 1001)]
public class PostModel : PageModel
{
...
顺便说一句,如果您要发布 JSON,您需要在处理程序参数上使用 FromBody
属性,而不是 FromForm
。