asp.net-mvc – 构建与数据格式分离的ASP.NET MVC REST API的代码的最佳方法?
|
我在ASP.NET MVC中创建REST API.我希望请求和响应的格式是 JSON或XML,但是我也想让它更容易添加另一种数据格式,并且很容易先创建XML并稍后添加JSON. 基本上我想指定我的API GET / POST / PUT / DELETE请求的所有内部工作方式,而不必考虑数据的格式或它将留下的内容,我可以稍后轻松指定格式或更改它每个客户.所以一个人可以使用JSON,一个人可以使用XML,一个人可以使用XHTML.然后我可以添加另一种格式,而无需重写大量代码. 我不想在我的所有操作结束时添加一堆if / then语句并确定数据格式,我猜我有一些方法可以使用接口或继承等来做到这一点,只是不确定最好的方法. 解决方法序列化ASP.NET管道就是为此而设计的.控制器操作不会将结果返回给客户端,而是返回结果对象(ActionResult),然后在ASP.NET管道的后续步骤中处理.您可以覆盖ActionResult类.请注意,FileResult,JsonResult,ContentResult和FileContentResult是MVC3内置的. 在您的情况下,最好返回类似RestResult对象的东西.该对象现在负责根据用户请求(或您可能具有的任何其他规则)格式化数据: public class RestResult<T> : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
string resultString = string.Empty;
string resultContentType = string.Empty;
var acceptTypes = context.RequestContext.HttpContext.Request.AcceptTypes;
if (acceptTypes == null)
{
resultString = SerializeToJsonFormatted();
resultContentType = "application/json";
}
else if (acceptTypes.Contains("application/xml") || acceptTypes.Contains("text/xml"))
{
resultString = SerializeToXml();
resultContentType = "text/xml";
}
context.RequestContext.HttpContext.Response.Write(resultString);
context.RequestContext.HttpContext.Response.ContentType = resultContentType;
}
}
反序列化 这有点棘手.我们正在使用Deserialize< T>基类控制器类的方法.请注意,此代码未准备好生产,因为读取整个响应可能会使服务器溢出: protected T Deserialize<T>()
{
Request.InputStream.Seek(0,SeekOrigin.Begin);
StreamReader sr = new StreamReader(Request.InputStream);
var rawData = sr.ReadToEnd(); // DON'T DO THIS IN PROD!
string contentType = Request.ContentType;
// Content-Type can have the format: application/json; charset=utf-8
// Hence,we need to do some substringing:
int index = contentType.IndexOf(';');
if(index > 0)
contentType = contentType.Substring(0,index);
contentType = contentType.Trim();
// Now you can call your custom deserializers.
if (contentType == "application/json")
{
T result = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(rawData);
return result;
}
else if (contentType == "text/xml" || contentType == "application/xml")
{
throw new HttpException(501,"XML is not yet implemented!");
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-core – 如何在ASP.NET Core 2.0中实现machineKey
- asp.net – 将MemoryStream文件存储到Azure Blob
- 使用ASP.NET WebApi中的HttpClient异步读取分块内容
- asp.net – .NET“代码块块”?
- asp.net-mvc – Visual Studio:您使用什么方法为类似项目“
- Asp:文本框与输入文本(PHP开发人员学习ASP)
- Asp.net Webservice – 使用jquery AJAX安全地调用webservi
- asp.net-mvc – 具有DateTime的MVC 3编辑器模板
- asp.net – 我可以在服务器端调用CustomValidator方法而无需
- asp.net-mvc – 如何在ASP.NET MVC中禁用客户端和代理缓存?
- asp.net – .net MVC将linq数据从控制器传递到视
- asp.net-mvc-5 – 如何在没有数据库的情况下使用
- asp.net-mvc-3 – ASP.NET MVC 3/4是否有任何响应
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之后
- 通过LAN调试ASP.NET云项目
- asp.net – 登录尝试后GoDaddy上的另一个安全例外
- asp.net-mvc – ASP.NET MVC会话超时,绝对还是滑
- 标签 – 如何使用像asp这样的Razor:Literal?
- asp.net-mvc – 如何在ASP.net MVC 4 RouteConfi
- asp.net-mvc – 自定义DateTime模型绑定在Asp.ne
