在Mvc中创建WebApi是所遇到的问题
1.提示"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; "类似这种的异常
解决方法:
在"WebApiConfig.cs"中添加如下代码
方式一(在"Register"方法中插入如下代码)亲自测试可以:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
config.Formatters.Remove(config.Formatters.XmlFormatter);
方式二(这个没有测过):
public static class WebApiConfig {
public static void Register (HttpConfiguration config) {
JsonMediaTypeFormatter jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().Single();
jsonFormatter.UseDataContractJsonSerializer = false;
jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
jsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
}
}
详情参考:http://stackoverflow.com/questions/21046872/prevent-id-ref-when-serializing-objects-using-web-api-and-json-net
在Mvc中创建WebApi是所遇到的问题的更多相关文章
- NET Core MVC中创建PDF
使用Rotativa在ASP.NET Core MVC中创建PDF 在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP ...
- mvc中的webapi
MVC中 webapi的使用 和 在其他网站中如何来调用(MVC) 1.webapi的路由规则注册在App_Start\WebApiConfig.cs文件中 2.webapi控制器继承父类 apiCo ...
- 使用Rotativa在ASP.NET Core MVC中创建PDF
在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP.NET MVC,那么Rotativa工具已经可用,我们可以使用 ...
- C#MVC中创建多模块web应用程序
当一个应用程序有越来越多的子模块后,应用程序将变得越来越大,复杂度也越来越高,应用程序也越来越难维护.如果把每个子模块,独立分成不同的web应用程序,则这个项目将易于维护.关于这个的好处,我也描述得不 ...
- 在 ASP.NET MVC 中创建自定义 HtmlHelper
在ASP.NET MVC应用程序的开发中,我们常碰到类似Html.Label或Html.TextBox这样的代码,它将在网页上产生一个label或input标记.这些HtmlHelper的扩展方法有些 ...
- asp.net mvc项目创建WebApi简单例子
1.创建默认路由的映射. namespace RedisDemo.App_Start { public class WebApiConfig { public static void Register ...
- 在ASP.NET MVC中创建自定义模块
创建模块 module是实现了System.Web.IHttpModule接口的类.该接口定义了两个方法: Init:当模块初始化时被调用,传入的参数为HttpApplication对象,用于注册请求 ...
- MVC 中创建自己的异常处理
1.新建类一个类继承 HandleErrorAttribute 类把异常书写到队列中 public class MyExceptionAttribute: HandleErrorAttribute { ...
- asp.net mvc中包含webapi时,token失效产生302的解决方案
public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOpt ...
随机推荐
- KEIL C51的XBYTE关键字
The XBYTE macro allows you to access individual bytes in the external data memory of the 8051. You m ...
- 14.4.1 Buffer Pool
14.4.1 Buffer Pool buffer pool 是一个主人的内存区域 InnoDB caches 表和index 数据. buffer pool 允许经常访问的数据直接从内存里处理,加快 ...
- 【HDOJ】2809 God of War
状态DP. /* 2809 */ #include <iostream> #include <queue> #include <cstdio> #include & ...
- 【转】Derivation of the Normal Equation for linear regression
I was going through the Coursera "Machine Learning" course, and in the section on multivar ...
- ubuntu14.04.2 添加ppa remastersys源 镜像ubuntu系统
- Firefox中firebug和xpath checker工具的使用
一直想把自己这段时间做的东西整理下,确迟迟没有动手,现在信息抽取工作已经做的差不多,把自己感觉很好用的两个工具介绍给大家吧! Firefox真是一个好东西,它许多插件.本人是很讨厌插件的,每次电 ...
- poj 3271 Lilypad Pond bfs
因为有了1的存在,使得问题变得比较难搞了,所以比较简单的做法就是把1去掉,先做一次bfs,处理出每个点能够一步到达的点(一定是1步). 然后就可以在新图上用bfs算出两个点之间的最短路,和最短路的个数 ...
- 高效JQuery编码
缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); ...
- setWillNotDraw和setFillViewport
Romain Guy write a little info about a ScrollView attribute that is missing from documentation : An ...
- hdu 5108
//题意是给一个数N,然后让你求M,使得N/M为素数,并且M的值最小 //思路呢,大概有两种,一个是遍历素数求解的,不过数据太大不现实 //另外一种就是质因数求解,for循环是遍历质因数,然后whil ...