[HttpPost]
[Route("[action]")]
public string GetString()
{
return "abc";
}
When I use the method above in an asp.net core controller the result is not formatted as json and the content type is text/plain. While every non-primitive result type is indeed formatted as json. How can I achive the same for string as return type?
3
Answers
You can wrap it in an
Ok
ActionResult.(It might work without the
Ok
as we also changed the return type toActionResult<string>
)Your return value is not a valid json, but you can do the following to change the content type only
Or you can inherit from
Controller
instead ofControllerBase
your "abc" is not valid json string and it doesnt make any sense to return an invalid json string as a json content.
but if you want to return as an action output a valid json string, for example
you can use this syntax
or if you use a minimal API