asp.net webapi 返回json结果的方法
第一种:
public static void Register(HttpConfiguration config) {
//1、将默认的xml格式化程序清除
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
//2、设置默认返回json格式的数据
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("dataType","json","application/json"));
//3、如果设置http://xxx?dataType=xml,则返回xml格式的数据
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("dataType", "xml", "application/xml"));
// Web API 配置和服务
// Web API 路由
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Action的使用方式:
[AcceptVerbs("GET", "POST")]
[Route("FetchList")]
public Resultx FetchList() {
return new Resultx { Code = , Message = "OK", Result = new { name="list",list=new List<string> { "",""} } };
}
第二种:
[AcceptVerbs("GET", "POST")]
[Route("FetchList")]
//使用IHttpActionResult接口,用JsonResult<>()来返回Json字符串
public IHttpActionResult FetchList() {
return Json(new { Code = , Message = "OK", Result = new { name = "list", list = new List<string> { "", "" } } });
}
以上两种结果都能正常返回Json字符串:
{"Code":0,"Message":"OK","Result":{"name":"list","list":["1","2"]}}
asp.net webapi 返回json结果的方法的更多相关文章
- 如何让Asp.net webAPI返回JSON格式数据
ASP.NET Web API 是新一代的 HTTP 網路服務開發框架,除了可以透過 Visual Studio 2012 快速開發外 (內建於 ASP.NET MVC 4 的 Web API 專案範 ...
- WebApi返回Json格式字符串
WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...
- webapi返回json格式优化
一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.For ...
- (转)WebApi返回Json格式字符串
原文地址:https://www.cnblogs.com/elvinle/p/6252065.html WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉 ...
- WebAPI搭建(二) 让WebAPI 返回JSON格式的数据
在RestFul风格盛行的年代,对接接口大多数人会选择使用JSON,XML和JSON的对比传送(http://blog.csdn.net/liaomin416100569/article/detail ...
- webapi返回json格式优化 转载https://www.cnblogs.com/GarsonZhang/p/5322747.html
一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 1 config.Formatters.Remove(config.F ...
- webapi返回json格式,并定义日期解析格式
1.webapi返回json格式 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferen ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- ASP.NET Core 返回 Json DateTime 格式
ASP.NET Core 返回 Json 格式的时候,如果返回数据中有DateTime类型,如何自定义其格式呢?配置如下: services.AddMvc().AddJsonOptions(opt = ...
随机推荐
- legend2---开发日志2(注释和函数比较好的写法)
legend2---开发日志2(注释和函数比较好的写法) 一.总结 一句话总结: 函数用_接意群 注释的关键字用[]括起来 注释的步骤用中文的步骤二字 1.为何以步骤为名写注释? 结构非常清晰 //步 ...
- inflate()引发NullPointerException
有时候我们在infalete的时候明明什么都对为什么它会提示出错 原意是你的资源layout出错了 注意看有没有把View写成view 这个View应该大写!V而不是小写v 踩坑踩了两次了!上次以为是 ...
- Hadoop-2.3.0的Eclipse插件编译
Hadoop-2.3.0的Eclipse插件编译 #cd /usr/local/src/hadoop2x-eclipse-plugin-master/src/contrib/eclipse-plugi ...
- Confluence 6 对一个空间进行归档后产生的影响
空间 如果一个空间被归档: 将不会在查找结果中显示,除非你选择 在归档空间中查找(Search archived spaces).如果没有归档空间的话,这个功能是隐藏的. 页面和内容将不会在 Conf ...
- git分支重命名
git branch - m old_name new_name
- mysql命令查询
含义 命令 查看gtid是否开启 show variables like '%gtid%'; 查看只读信息 show global variables like "%read_only%& ...
- 【CSS】【1】让DIV中的文字换行显示
<div style="white-space:normal;word-break:break-all;word-wrap:break-word;">data</ ...
- poj-2888-矩阵+polya
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 6195 Accepted: 1969 D ...
- URL与URI的含义及区别
1.1 什么是URI? 简单点说:URI就是通用资源标志符,不理解是吧,我第一次听说也是不理解. 进一步说:网络上的一些资源(文档.图片.音频.视频.程序等)都是有一些通用资源标识(Universal ...
- Python爬虫有道翻译接口
import urllib.request import urllib.parse import json import hashlib from datetime import datetime i ...