C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte转载:http://www.itdos.com/Mvc/20150302/0741255.htmlusing System.IO; ///<summary>/// WebApi返回图片 ///</summary>public HttpResponseMessage GetQrCode() { var imgPath = @"D:\ITdosCom\Images\itdos.jpg"; //从图片中读取byte var imgByte = File.ReadAllBytes(imgPath); //从图片中读取流 var imgStream = new MemoryStream(File...
在全局设置webapi设置json返回类型首字母小写:config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();但是想针对个别api设置默认的json格式,怎么办呢?下面是一种方案:第一步:自定义一个消息处理程序/// <summary>/// 自定义http消息处理程序/// </summary>public class MessageHandlerOne : DelegatingHandler{protected override Task<HttpResponseMessage>...
先看效果,注意:(1)这里由于我的模拟器不支持中文输入,所以,对于这张效果图,我是直接在代码中写死了我的查询城市,在下面的代码中我是没有把要查询的城市写死的。 (2)读者要想成功使用本示例的所有代码的话(就是一成不改,这也太懒了哈!),需要读者自己申请一个key,然后放入我的代码中,下面的代码注释中我也有说道,请认真观看。 (3)改代码本人亲测可行,有图有真相。 ...
两种转换方式:1.全局设定,针对GlobalConfiguration.Configuration.Formatters.JsonFormatter做设定,位于Global.asax 如:var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); json.SerializerSettings.NullValueHandling = Newton...
一、Global配置(此方法杀伤力太大,所有的返回的xml格式都会被毙掉) public class WebApiApplication : System.Web.HttpApplication{protected void Application_Start(){AreaRegistration.RegisterAllAreas();GlobalConfiguration.Configure(WebApiConfig.Register);FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles...
[ActionName("CreateEmployeeTrackRecord")] public HttpResponseMessage PostCreateEmployeeTrackRecord([FromBody]Object json) { try { var jInfo = SerializeJsonToDynamic(json.ToString()); int userId = Convert.ToInt32(jInfo.UserId.Value); int factoryId = Convert.ToInt32(jInfo.FactoryId.Value); DateTime signIn...
async Task<IActionResult> PutModel(DTO model) {_context.Entry(model).State = EntityState.Modified;var result = await _context.SaveChangesAsync().ConfigureAwait(false);return OK("修改实体成功!"); }这样做后台是很方便,可是EF全部更新效率实在太低,而且没必要,而且前台就需要把所有的DTO对象都传过来,如果有不想要修改的怎么办?好吧。用EF的 _context.Entry(model).Property(m => m.Field1).IsModified = fals...
在页面想webApi post json数据的时候,发现webapi不能直接以json的方式接受数据(注:我是没有发现一个很好的方式来post json数据的);但是可以以数据结构的方式传递; 如下: //js代码 var d = {Id: "1",Name: "name",Value: "OldValue", 7 };$.ajax({type: "post",url: url1,data: JSON.stringify({pConfig: d}),success:function(d){}}); public class Diff{public string Id { set; get; }public string Name { set; get...
1. 文章目的随着 WebApiClient 的不断完善,越来越多开发者选择WebApiClient替换原生的HttpClient,本文将介绍使用WebApiClient来完成JsonPatch提交的新特性。 2. json patch介绍在服务端WebApi开发的时候,如果设计一个更新登录用户的个人信息的接口,用户能更新昵称、密码、生日、性别等N多个字段,但用户在实际使用中,可能是更新其中的一个或不确定多个字段。开发者往往会为难:对于客户端没有更新到的字段,应该设置为什么值呢...
转自:https://www.cnblogs.com/qidian10/p/6074537.html https://www.cnblogs.com/zhong-ken/p/6062782.html https://q.cnblogs.com/q/85812 在 网站的 ConfigureServices 方法中,在配置 AddControllers 后,就可以 AddJsonOptions 了: public void ConfigureServices(IServiceCollection services) {// 添加 Json 格式化配置(在开发时方便使用)services.AddControllers().AddJsonOptions((options => options.JsonSerializ...
假设ApiController的动作具有多个参数:[HttpPost] public void AddAddress( Person person, Address address, int simple )或类似的东西.现在,我尝试发送内容类型为application / json和json的发布请求,如下所示:{person: {...}, address: {..}, simple: 1 }只是假设它是有效的json;) 或像这样的json数组中: [人,地址,简单] 但是WebApi无法识别这一点,并告诉我它找不到操作,因为它不支持json请求中的多个参数. 我正在使用Htt...
我找不到将DateTimeOffset值获取到JavaScript(angular2)的好方法.我正在使用WebApi(5.2.3)和angular2.在电线上,我看到的日期如下:RecordModifiedAt : "2016-03-08T17:27:11.9975483+01:00"JavaScript / angular2无法将此识别为有效的datetime值. 我确实有选择,但是我应该去哪个方向: >服务器端:Newtonsoft.Json,…>客户端:angular2,…>其他? 非常感谢您的帮助!解决方法:感谢我对PierreDuc的反馈,我得出以下结论: 由于JSON不...
我正在创建一个Web Api方法,该方法应接受JSON对象和简单类型.但是所有参数始终为空. 我的json看起来像{ "oldCredentials" : {"UserName" : "user","PasswordHash" : "myCHqkiIAnybMPLzz3pg+GLQ8kM=","Nonce" : "/SeVX599/KjPX/J+JvX3/xE/44g=","Language" : null,"SaveCredentials" : false }, "newPassword" : "asdf"}我的代码如下所示:[HttpPut("UpdatePassword")] [Route("WebServices/UsersService.svc/rest/users/user")] pu...
我的WebAPI应用程序中有一个模型,用.NET 4.0编写,具有System.Net.Mime.ContentType类型的属性,如下所示:[Serializable] public class FileData {private ContentType contentType;private long size;private string name;public ContentType ContentType{get { return contentType; }set { contentType = value; } }.../* same getter/setter logic for the other fields */ }该模型位于与我的Web项目不同的程序集中. 所以,客户...
using System.IO;/// <summary> /// WebApi返回图片 /// </summary> public HttpResponseMessage GetQrCode() {var imgPath = @"D:\ITdosCom\Images\itdos.jpg";//从图片中读取bytevar imgByte = File.ReadAllBytes(imgPath);//从图片中读取流var imgStream = new MemoryStream(File.ReadAllBytes(imgPath));var resp = new HttpResponseMessage(HttpStatusCode.OK){Content = new ByteArrayContent(imgByte)//或者//Content = new...