Passsing Model RedirectToAction without DataTemp, Unsupported Media Type : status 415

Bagoes Heikhal 1 Reputation point
2021-11-22T09:03:44.047+00:00

At my Accounts controller, I want to call the Get Profile method using RedirectToAction. I read some StackOverflow that said to use DataTemp[""], but the DataTemp method is not found.

   [HttpPost("Login")]
    public ActionResult Post(LoginVM loginVM)
    {
        var result = accountRepository.Login(loginVM);
        switch (result)
        {
            case 1:
                return RedirectToAction("GetProfile","Accounts", loginVM);
            default:
                return BadRequest(new { status = HttpStatusCode.BadRequest});
        }
    }

   [HttpGet("Profile")]
    public ActionResult GetProfile(LoginVM loginVM)
    {
        var result = accountRepository.Profile(loginVM);
        if (result.Count() != 0)
        {
            return Ok(new { status = HttpStatusCode.OK, result = result});
        }
        return NotFound(new { status = HttpStatusCode.NotFound});
    }

this is the error when I try to input to post man, I'm already use Json type to post

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "00-843c7cb9a3c8d04ca7ca174758086054-e86c66c3adb00b40-00"
}

my repo type

   public IEnumerable<ProfileVM> Profile(LoginVM loginVM).ToList()
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
1,200 questions
No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 9,281 Reputation points Microsoft Employee
    2021-11-23T07:29:29.61+00:00

    Hi @Bagoes Heikhal ,

    Unsupported Media Type

    The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.
    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.
    You use Postman for testing, please try to add this section to Headers: Content-Type: application/json
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 28,276 Reputation points
    2021-11-23T15:54:34.507+00:00

    The action is a get, you can not send json. The parameters should be passed as query string parameters.

    No comments

  3. AgaveJoe 17,216 Reputation points
    2021-11-24T15:16:53.53+00:00

    As explained by Bruce-SqlWork, your design submits a GET. GET parameters are passed in the query string.

    [HttpPost("Login")]  
    public ActionResult Post([FromBody]LoginVM loginVM)  
    {  
        var result = accountRepository.Login(loginVM);  
        switch (result)  
        {  
            case 1:  
                return Redirect($"/Profile/?Username={loginVM.Username}&Password={loginVM.Password}");  
            default:  
                return BadRequest("Post");  
        }  
    }  
    [HttpGet("Profile")]  
    public ActionResult GetProfile(string Username, string Password)  
    {  
        LoginVM loginVM = new LoginVM() { Username = Username, Password = Password };  
        var result = accountRepository.Profile(loginVM);  
        if (result.Count() != 0)  
        {  
            return Ok(new { result = result });  
        }  
        return NotFound("GetProfile");  
    }  
    

    Do not pass login credentials in the URL. The standard approach is to populate an authentication cookie. Please see the following tutorial.

    Use cookie authentication without ASP.NET Core Identity

    No comments