1

I got this simple request to an action in an asp.net core controller.

Controller:

[ApiController]
public class SystemAPIController : ControllerBase
{
    [HttpPost("systemAPI/SetCulture")]
    public async Task SetCulture([FromBody] string culture)
    {
        this.HttpContext.Session.SetString("Culture", culture);
    }
}

The JSON in the body:

{
    "culture": "nb-NO"
}

I get the following error when invoking the action using postman:

        "The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
1
  • We need to see the text response to help solve issue. The response is not matching the your expect controller response. Commented Jan 31, 2021 at 22:19

1 Answer 1

2

You need to create a class which will represent the json structure:

public class Request
{
    public string culture {get;set;}
}

And use it as incoming action parameter:

[HttpPost("systemAPI/SetCulture")]
public async Task SetCulture([FromBody] Request request)
{
    this.HttpContext.Session.SetString("Culture", request.culture);
}

Also common naming convention for properties in C# is PascalCasing but to support it you will either set up it globally or use JsonPropertyNameAttribute per property:

public class Request
{
    [JsonPropertyName("culture")]
    public string Culture {get;set;}
}
Sign up to request clarification or add additional context in comments.

2 Comments

created the structure at it worked perfect
@SteinTheRuler was glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.