我有一个ASP.NET MVC WEB API。出于多种原因(例如因为没有授权而重定向等),我不能仅使用一个简单的对象并在我的控制器方法中返回它。因此,我需要使用HttpResponseMessage类来进行重定向。
目前,我的做法是:
var response = new Response { responseCode = Response.ResponseCodes.ItemNotFound };
var formatter = new JsonMediaTypeFormatter();
response.Content = new ObjectContent<Response>(response, formatter, "application/json");
如何将对象序列化为JSON格式,并将其放入HttpResponseMessage的内容中。但我感觉还有一种更好的方法。你有什么想法吗?
..将对象序列化为JSON格式,并将其放入HttpResponseMessage的内容中。但我感觉还有一种更好的方法。你有什么想法吗?