c# - ASP.NET Core HTTPRequestMessage returns strange JSON message -
i working asp.net core rc2 , running strange results. have mvc controller following function:
public httpresponsemessage tunnel() { var message = new httpresponsemessage(httpstatuscode.ok); message.content = new stringcontent("blablabla", encoding.utf8); message.content.headers.contenttype = new system.net.http.headers.mediatypeheadervalue("text/plain"); message.headers.cachecontrol = new system.net.http.headers.cachecontrolheadervalue { nocache = true }; return message; }
if call postman accept header set text plain response:
{ "version": { "major": 1, "minor": 1, "build": -1, "revision": -1, "majorrevision": -1, "minorrevision": -1 }, "content": { "headers": [ { "key": "content-type", "value": [ "text/plain" ] } ] }, "statuscode": 200, "reasonphrase": "ok", "headers": [ { "key": "cache-control", "value": [ "no-cache" ] } ], "requestmessage": null, "issuccessstatuscode": true }
i not understand how generated reponse above controller. json serialization of entire message , in no way contain "blablabla" intended send.
the way have gotten desired result making controller function return string
instead of httpresponse
, way unable set headers cachecontrol
so question is: why strange response? seems weird behaviour me
according this article, asp.net core mvc not support httpresponsemessage
-returning methods default.
if want keep using it, can, using webapicompatshim:
- add reference
microsoft.aspnetcore.mvc.webapicompatshim
project. - configure in
configureservices()
:services.addmvc().addwebapiconventions();
set route in
configure()
:app.usemvc(routes => { routes.mapwebapiroute( name: "default", template: "{controller=home}/{action=index}/{id?}"); });
Comments
Post a Comment