C
C#

help

❔ Unsupported Media Type - Razor page

Ppopulus2/3/2023
{"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.
I haven't run into this type of problem before. Setting breakpoints in Visual Studio isn't triggering the breakpoints. It just shows me the first code-block provided in the browser. Here's the "Pages/Account/Register.cshtml":
@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>
}
}
Ppopulus2/3/2023
Here's the "AccountController":
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();
}
}
Here's the "LoginModel":
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; }
}
}
Here's the "Account"-model:
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;
}
}
}
CChucklesTheBeard2/4/2023
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
AAccord2/5/2023
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
✅ JsonConvert debugging helpHello! I'm having a hard time figuring out how to solve an issue where JsonConvert.DeserializeObject❔ Make a sprite follow mouse while pivoting around character in UnityHi, I its hard to describe my issue by describing it since im not sure exactly what I'm looking for ❔ is Generic Repository a bad implementation of Repository Pattern?Surfing the internet I have found several articles that say it is an anti-pattern because it is an a❔ Problems with using Dotnet 6 on Windows to publish to an exe formatDotnet/C# newb here. Previously I've only done a small amount of development using the **.Net Framew❔ Using net7 documentation while targeting netstandard2.0This might be a bit of a dumb question, but is there anyway to make omnisharp/roslyn's tooltips use ❔ WPF app unit testingI am learning how to make unit tests. I built my app on .net core 6.0 and I created class library (o❔ where exactly should i put authorizationso i read a few posts on where to put authorization and most of the people agree on putting it in th❔ C# WPF playing background musicHello, I have WPF app for game launcher/updater. I wanted to add background music, so I added this cC# wpf mvvm Datagrid filter using LINQSo here I have a MVVM form. the Form contains a Datagrid which is connected to the Databank. I also ❔ Is it worth to create lazy dictionary in this case?I have following class: so as you see Add function add to the dictionary class TreeNode, it looks li❔ Going to use a listbox with a switch for something in the morning, need help with format for bothI would like to set two variables with a listbox using a switch and one variable with a second listb✅ What's your view on tool like Sonarqube?Should I learn from the code smells it captures?