C# Razor页面将表单数据发布到控制器
我使用WebAPI模板在.NETCore2.2中开发了一个非常简单的web应用程序。我已经能够使用Postman成功地测试我的端点,并且希望使用已经可用的Razor页面(.cshtml)添加一个简单的前端 问题: 我无法使用razor页面成功命中控制器端点。我已尝试使用/不使用“[FromBody]”来装饰端点 我的chrome调试器捕获以下事务:C# Razor页面将表单数据发布到控制器,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我使用WebAPI模板在.NETCore2.2中开发了一个非常简单的web应用程序。我已经能够使用Postman成功地测试我的端点,并且希望使用已经可用的Razor页面(.cshtml)添加一个简单的前端 问题: 我无法使用razor页面成功命中控制器端点。我已尝试使用/不使用“[FromBody]”来装饰端点 我的chrome调试器捕获以下事务: 网页上的错误: {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","tit
网页上的错误:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"0HLSP5DVTEH9B:00000008"}
输出表示路由已匹配:
代码:我有以下页面: 注册。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" />
}
}
控制器:
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.");
}
}
}
namespace RandomContent.controller
{
[授权]
[ApiController]
[路线(“注册”)]
公共类注册控制器:ControllerBase
{
私人只读IRegistrationService\u registrationService;
公共注册控制器(IRegistrationService registrationService)
{
_注册服务=注册服务;
}
///
///创建一个新用户
///如果无法将用户添加到数据库,则将返回错误的请求
///或者用户已经存在
///
///
///
///
[异名]
[HttpPost]
[路线(“登记”)]
公共IActionResult寄存器(RegistrationModel userParam)
{
//验证
如果(!Enum.IsDefined(typeof(Role),userParam.Role))
return BadRequest(“必须为用户选择有效角色”);
if(string.IsNullOrWhiteSpace(userParam.Username))
返回请求(“必须输入用户名”);
if(string.IsNullOrWhiteSpace(userParam.Password))
返回请求(“必须输入密码”);
//注册用户
var user=\u registrationService.Register(userParam);
如果(user!=null)返回Ok(user);
return BadRequest(“无法创建用户配置文件。他们的用户名可能已被占用,或者发生了其他事情。”);
}
}
}
问题:如何将表单数据成功发布到控制器中?我甚至不能调试到我的控制器,除非我从邮递员发送我的请求
可以在Github上找到完整的项目:尝试使用[FromForm]这样的注册表([FromForm]RegistrationModel userParam)这是否回答了您的问题?
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.");
}
}
}