I see a lot of samples on the web for ASP.NET Web API that use Request.CreateResponse
to generate responses. If you create a new Web API project in Visual Studio, the default API controllers return IEnumerable<string>
or string
. What's the difference in usage?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Returning a HttpResponseMessage
allows you to control all different aspects of the returned HTTP response directly. If you return a POCO you have to hope that the framework does what you want. And maybe it will, and maybe it won't.
回答2:
Request.CreateResponse can generate other http message, ex.404
,500
.
If just return,the http message will be 200 OK
.
In my code, if get need authorized, I will return like this:
throw new HttpResponseException(HttpStatusCode.Unauthorized);
write this,you unnecessary change the return value.
标签:
asp.net-web-api