【问题标题】:Razor Page POST form data to controllerRazor Page POST 表单数据到控制器
【发布时间】:2020-01-14 20:04:01
【问题描述】:

我使用 WebAPI 模板在 .NET Core 2.2 中开发了一个非常简单的 Web 应用程序。我已经能够使用 Postman 成功测试我的端点,并希望使用已经可用的 Razor 页面 (.cshtml) 添加一个简单的前端

问题: 我无法使用我的剃须刀页面成功点击我的控制器端点。我尝试使用/不使用“[FromBody]”来装饰我的端点。

我的 chrome 调试器捕获了以下事务:

网页错误:

{"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" />
    }
}

Registration.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

【问题讨论】:

标签: c# asp.net-mvc razor


【解决方案1】:

尝试像这样使用 [FromForm] Register([FromForm] RegistrationModel userParam)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2013-12-31
    相关资源
    最近更新 更多