aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Fix attempt of 3959 (https://github.com/aspnet/AspNetCore/issues/3959) #8719

Closed sobanieca closed 5 years ago

sobanieca commented 5 years ago

This PR is an attempt to fix an issue 3959 (link) reported in AspNetCore repository. However, I need some help :)

    [HttpPost]
    public IActionResult DisplayName([FromBody] Person person)
    {
        return Ok(person.Name);
    }

    public class Person
    {
        public string Name { get; set; }
    }

And used similar powershell as the one from issue repro steps (https://github.com/sobanieca/AspNetCoreEncodingIssue)

...
    $headers = @{
        Accept="application/json"
        "Content-Type"="application/json"
    }

    $content = Get-Content "Request.json" -Raw #Non UTF-8 encoded file

    $data = $content

    $url = "http://localhost:49967/Login/DisplayName"

    $result = Invoke-WebRequest -Uri $url -Method POST -Headers $headers -Body $data -UseBasicParsing 
    $responsecode= $result | select StatusCode

    write-host "Response code: " + $responsecode.StatusCode
...

However, in response it gives me 204 status code. I would expect to see 400 - Bad Request here (with the code from PR). I did some more digging and changed Powershell so I'm sending XML instead of JSON:

...
    $headers = @{
        Accept="application/json"
        "Content-Type"="application/json"
    }

    $data = "<Name>abc</Name>"

    $url = "http://localhost:49967/Login/DisplayName"

    $result = Invoke-WebRequest -Uri $url -Method POST -Headers $headers -Body $data -UseBasicParsing 
    $responsecode= $result | select StatusCode

    write-host "Response code: " + $responsecode.StatusCode
...

I would expect to see 400 - Bad Request here as well. But instead, I've seen 204 again. Is there any issue with 3.* MVC, or I'm just missing something?

dnfclas commented 5 years ago

CLA assistant check
All CLA requirements met.

sobanieca commented 5 years ago

Regarding point 3 - I've just created AspNetCore 2.1.* project from template in VS and when running request with "application/json" content and some dummy xml inside I'm getting BadRequest out-of-the-box (as expected). So I wonder, why it's not working for MvcSandbox application... Is it because of lack of .UseMvc() instruction in startup.cs?

mkArtakMSFT commented 5 years ago

Thanks for your effort, @sobanieca. As part of the repo reduction effort (https://github.com/aspnet/Announcements/issues/330) we're closing all the PRs in this repo. Please reopen this PR against the AspNetCore repo so we can consider it.