I have developed a very simple web app in .NET Core 2.2 using the WebAPI template. I have been able to successfully test my endpoints using Postman and would like to add a simple frontend using the already available Razor Pages (.cshtml)
Problem: I cannot successfully hit my controller endpoint using my razor page. I have tried decorating my endpoint with/without "[FromBody]".
My chrome debugger catches the following transaction:
Error on Webpage:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"0HLSP5DVTEH9B:00000008"}
Output says that the route was matched:
Code:
I have the following razor page: 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; }
}
}
Controller:
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.");
}
}
}
Question: How can I successfully post my form data into my controller? I can't even debug into my controller unless I send my request from postman.
The full project can be found on Github: https://github.com/bestfriendkumar/RandomContent