c# - Razor Page POST 表单数据到控制器
问题描述
我使用 WebAPI 模板在 .NET Core 2.2 中开发了一个非常简单的 Web 应用程序。我已经能够使用 Postman 成功测试我的端点,并且想使用已经可用的 Razor 页面 (.cshtml) 添加一个简单的前端
问题: 我无法使用我的剃须刀页面成功访问我的控制器端点。我尝试使用/不使用“[FromBody]”来装饰我的端点。
网页错误:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"0HLSP5DVTEH9B:00000008"}
代码:
我有以下剃须刀页面: Registration.cshtml
@page
@using Microsoft.AspNetCore.Mvc.Rendering
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model RegistrationModel
@{
ViewData["Title"] = "Registration";
}
<h2>@ViewData["Title"]</h2>
@{
using (Html.BeginForm("Register", "Registration", FormMethod.Post))
{
<label>Username</label>
@Html.TextBoxFor(m => m.Username)
<br />
<label>Password</label>
@Html.TextBoxFor(m => m.Password)
<br />
<label>Role</label>
<label>
@Html.RadioButtonFor(m => m.Role, "0")
1
</label>
<label>
@Html.RadioButtonFor(m => m.Role, "1")
2
</label>
<br />
<input type="submit" value="Register" />
}
}
注册.cshtml.cs
namespace RandomContent.Pages
{
public class RegistrationModel : PageModel
{
public string Message { get; set; }
public void OnGet()
{
Message = "Fill out the form below to be registered with the application.";
}
[Required]
[Display(Name = "User name")]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Role")]
public Role Role { get; set; }
}
}
控制器:
namespace RandomContent.Controllers
{
[Authorize]
[ApiController]
[Route("Registration")]
public class RegistrationController : ControllerBase
{
private readonly IRegistrationService _registrationService;
public RegistrationController(IRegistrationService registrationService)
{
_registrationService = registrationService;
}
/// <summary>
/// Creates a new user
/// Will return a bad request if the user could not be added to the db
/// or the user already exists
/// </summary>
/// <param name="userParam"></param>
/// <param name="returnUrl"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost]
[Route("Register")]
public IActionResult Register(RegistrationModel userParam)
{
//Validations
if (!Enum.IsDefined(typeof(Role), userParam.Role))
return BadRequest("Must select valid Role for user");
if (string.IsNullOrWhiteSpace(userParam.Username))
return BadRequest("Must enter a username");
if (string.IsNullOrWhiteSpace(userParam.Password))
return BadRequest("Must enter a password");
//Register user
var user = _registrationService.Register(userParam);
if (user != null) return Ok(user);
return BadRequest("Could not create user profile. They username may have been taken, or something else happened.");
}
}
}
问题:我怎样才能成功地将我的表单数据发布到我的控制器中?除非我从邮递员那里发送请求,否则我什至无法调试到我的控制器中。
完整的项目可以在 Github 上找到:https ://github.com/bestfriendkumar/RandomContent
解决方案
尝试像这样使用 [FromForm] Register([FromForm] RegistrationModel userParam)
推荐阅读
- node.js - Socket.io 服务器在使用 heroku 部署时出现 405 错误
- nuxt.js - 在nuxt中使用自定义布局更改页面时如何修复“无效的组件名称”?
- java - 在 Spring Boot 中仅从 api 返回模型的特定字段
- python - 运行时错误:无法打开 shape_predictor_68_face_landmark.dat(无法执行脚本主程序)
- python - 如何将传递类添加到类参数并放入列表
- c# - 为什么此 Azure 函数项目会生成错误:未注册绑定类型“serviceBusTrigger”?
- php - 如何在 php:7.4-fpm-alpine docker 容器中启用 xdebug?
- asp.net - .Net Core 显示模型验证与验证过滤器
- python - 在 Django 3.1 中管理静态文件
- ios - 可编码结构中的 Firestore 字段值