❔ Unsupported Media Type - Razor page
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-de8ff040b623653dab0b747f6f7991ff-34ebbb1f01794d2d-00"}
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-de8ff040b623653dab0b747f6f7991ff-34ebbb1f01794d2d-00"}
The 415 (Unsupported Media Type) status code indicates that the
origin server is refusing to service the request because the payload
is in a format not supported by this method on the target resource.
The format problem might be due to the request's indicated
Content-Type or Content-Encoding, or as a result of inspecting the
data directly.
The 415 (Unsupported Media Type) status code indicates that the
origin server is refusing to service the request because the payload
is in a format not supported by this method on the target resource.
The format problem might be due to the request's indicated
Content-Type or Content-Encoding, or as a result of inspecting the
data directly.
@page
@{
ViewData["Title"] = "Register";
}
@model CommunityWebsite_Lexicon_Project.Models.LoginModel
@{
<h3>Register</h3>
<form asp-action="Register" asp-controller="Account" method="post">
<div asp-validation-summary="ModelOnly"></div>
<h4>Email</h4>
<input asp-for="Email" type="email" />
<span asp-validation-for="Email"></span>
<h4>Username</h4>
<input asp-for="Username" type="text" />
<span asp-validation-for="Username"></span>
<h4>Password</h4>
<input asp-for="Password" type="password" />
<span asp-validation-for="Password"></span>
<h4>Confirm Password</h4>
<input asp-for="PasswordConfirm" type="password" />
<span asp-validation-for="PasswordConfirm"></span>
<button type="submit" value="Register">Register</button>
</form>
}
@{
if (ViewBag.Message != null)
{
<div>
@ViewBag.Message
</div>
}
}
@page
@{
ViewData["Title"] = "Register";
}
@model CommunityWebsite_Lexicon_Project.Models.LoginModel
@{
<h3>Register</h3>
<form asp-action="Register" asp-controller="Account" method="post">
<div asp-validation-summary="ModelOnly"></div>
<h4>Email</h4>
<input asp-for="Email" type="email" />
<span asp-validation-for="Email"></span>
<h4>Username</h4>
<input asp-for="Username" type="text" />
<span asp-validation-for="Username"></span>
<h4>Password</h4>
<input asp-for="Password" type="password" />
<span asp-validation-for="Password"></span>
<h4>Confirm Password</h4>
<input asp-for="PasswordConfirm" type="password" />
<span asp-validation-for="PasswordConfirm"></span>
<button type="submit" value="Register">Register</button>
</form>
}
@{
if (ViewBag.Message != null)
{
<div>
@ViewBag.Message
</div>
}
}
3 Replies
Here's the "AccountController":
Here's the "LoginModel":
Here's the "Account"-model:
public async Task<IActionResult> Register(LoginModel submittedAccountModel)
{
if (ModelState.IsValid)
{
Account createdUser = new Account() // Create account object.
{
UserName = submittedAccountModel.Username,
Email = submittedAccountModel.Email,
};
Regex regex = new Regex(@"^[a-zA-Z0-9]+$"); // Only letters and digits allowed.
if (!regex.IsMatch(createdUser.UserName))
{
ModelState.AddModelError("NickName", "Only letters and digits are allowed in the NickName field.");
}
try // Setting the password.
{
await createdUser.SetPassword(submittedAccountModel.Password);
} catch (Exception)
{
throw new Exception("Error.");
} finally // Verify the password.
{
await createdUser.VerifyPassword(submittedAccountModel.PasswordConfirm);
}
IdentityResult result = await _userManager.CreateAsync( // Create the Account-Identity object.
createdUser, createdUser.PasswordHash
);
if (result.Succeeded)
{
await _accountRepository.AddAsync(createdUser); // Save the Account-Identity object.
return RedirectToAction("Login");
//return Ok();
} else
{
return View(submittedAccountModel);
}
} else
{
return BadRequest();
}
}
public async Task<IActionResult> Register(LoginModel submittedAccountModel)
{
if (ModelState.IsValid)
{
Account createdUser = new Account() // Create account object.
{
UserName = submittedAccountModel.Username,
Email = submittedAccountModel.Email,
};
Regex regex = new Regex(@"^[a-zA-Z0-9]+$"); // Only letters and digits allowed.
if (!regex.IsMatch(createdUser.UserName))
{
ModelState.AddModelError("NickName", "Only letters and digits are allowed in the NickName field.");
}
try // Setting the password.
{
await createdUser.SetPassword(submittedAccountModel.Password);
} catch (Exception)
{
throw new Exception("Error.");
} finally // Verify the password.
{
await createdUser.VerifyPassword(submittedAccountModel.PasswordConfirm);
}
IdentityResult result = await _userManager.CreateAsync( // Create the Account-Identity object.
createdUser, createdUser.PasswordHash
);
if (result.Succeeded)
{
await _accountRepository.AddAsync(createdUser); // Save the Account-Identity object.
return RedirectToAction("Login");
//return Ok();
} else
{
return View(submittedAccountModel);
}
} else
{
return BadRequest();
}
}
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;
namespace CommunityWebsite_Lexicon_Project.Models
{
public class LoginModel
{
public string? Username { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
public string? PasswordConfirm { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;
namespace CommunityWebsite_Lexicon_Project.Models
{
public class LoginModel
{
public string? Username { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
public string? PasswordConfirm { get; set; }
}
}
using Microsoft.AspNetCore.Identity;
namespace CommunityWebsite_Lexicon_Project.Models
{
public class Account : IdentityUser
{
public int Id { get; set; }
public string? UserName { get; set; }
public string? Email { get; set; }
private string Password { get; set; }
public async Task SetPassword(string password)
{
Password = new PasswordHasher<Account>().HashPassword(this, password);
}
public async Task<bool> VerifyPassword(string password)
{
var result = new PasswordHasher<Account>().VerifyHashedPassword(this, Password, password);
return result == PasswordVerificationResult.Success;
}
}
}
using Microsoft.AspNetCore.Identity;
namespace CommunityWebsite_Lexicon_Project.Models
{
public class Account : IdentityUser
{
public int Id { get; set; }
public string? UserName { get; set; }
public string? Email { get; set; }
private string Password { get; set; }
public async Task SetPassword(string password)
{
Password = new PasswordHasher<Account>().HashPassword(this, password);
}
public async Task<bool> VerifyPassword(string password)
{
var result = new PasswordHasher<Account>().VerifyHashedPassword(this, Password, password);
return result == PasswordVerificationResult.Success;
}
}
}
in your browser, hit ctrl+shift+c to open the devtools. Go over to the network tab then hit ctrl+f5. (this is the same in Firefox and Chrome) You should be able to work out which call is resulting in the 415 code, and inspect the request to see what the relevant attributes were set to
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.