1. 程式人生 > >.net MVC5 webAPI 返回json設定

.net MVC5 webAPI 返回json設定

namespace Learning.Web
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services


            // Web API routes
            config.MapHttpAttributeRoutes();


            config.Routes.MapHttpRoute(
                name: "Courses",
                routeTemplate: "api/courses/{id}",
                defaults: new {controller="courses", id = RouteParameter.Optional }
            );


            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();



        }
    }
}

相關推薦

.net MVC5 webAPI 返回json設定

namespace Learning.Web {     public static class WebApiConfig     {         public static void Register(HttpConfiguration config)        

WebApi返回型別設定json的三種方法

方法一:(改配置法)  找到Global.asax檔案,在Application_Start()方法中新增一句: GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Cl

.netwebapi 返回結果為json格式

在webapi專案的Global.asax檔案裡面,protected void Application_Start() 方法中,新增以下語句// 使api返回為json GlobalConfiguration.Configuration.Formatters.XmlForm

asp.net 重寫OnException返回json或跳轉新頁面

message ide string exce () toupper tpc exceptio tostring protected override void OnException(ExceptionContext filterContext)

C#WebAPI返回json去掉雙引號前面的反斜杠

去掉 eba resp body message pos ica new log string str="{\"msgType\":1001,\"msgstring\":\"信息\"}";//這裏是你的json帶有反斜杠的 HttpResponseMessage res

.Net Core WebApi返回日期格式的問題

環境:.net core 2.1 webapi 問題簡介: 返回DateTime,前端接收到的字元有時候為2018-01-01T12:01:01,有時候為2018-01-01T01:01:01.722+08:00,無法正常解析   原因: WebApi的Json解析採用的是NewtonSof

asp.net core WebApi 返回 HttpResponseMessage

ASP.NET WebApi 2 中的示例程式碼: [Route("values/{id}")] public async Task<HttpResponseMessage> Get(string id) { var response = Request.CreateResponse(H

ASP.NET core webapi返回資料型別 _平臺:windows (4)

ASP.NET core webapi 返回資料型別 _平臺:windows (4) 我們經常會發現前端大部分時候請求介面後,返回的資料都是json。那麼我們看看後面的api是如何寫的。 首先我們以預設建立的那個控制器(ValuesController)為參考物: using

netcore webapi返回json中null全域性替換為空字串

場景:資料庫中部分表字段允許空值,則程式碼中實體類對應的欄位型別為可空型別Nullable<>,如int?,DateTime?,null值欄位序列化返回的值都為null,前端對應欄位賦值需要做null值判斷,隨想到全域性把null替換為"" 網上找的方案都是重寫

【記錄一次坑經歷】axios使用x-www-form-urlencoded 伺服器報400(錯誤的請求。 )(後端.Net MVC5 WebApi OAuth,前端Electron-Vue)

首先放上原始碼 electron-vue axios 註冊 axios.defaults.baseURL = 'http://localhost:8888/' axios.defaults.headers.post['Content-Type'] = 'applicatio

asp.net MVC控制器返回json問題總結

在MVC專案中,經常需要在控制器中向客戶端返回json資料,常見的返回方法如下: 1、如果伺服器端完全採用微軟的類,則要序列號的集合類通常使用List<T>,返回時直接使用Json(list);則會自動將列表中的內容進行序列號,生成json字串。 2、如果伺服器

C#WebAPI返回json去掉雙引號前面的反斜槓

string str="{\"msgType\":1001,\"msgstring\":\"資訊\"}";//這裡是你的json帶有反斜槓的 HttpResponseMessage result = new HttpResponseMessage { Content =

ASP.NET Core WebAPI 開啟靜態網頁,設定主頁,跨域,Json保持原始大小寫,新增MIME,解除大檔案上傳限制

開啟靜態網頁 Startup.Configure: app.UseStaticFiles(); 設定主頁 Startup.Configure: DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();

C#.NET WebApi返回各種型別(圖片/json資料/字串),.net圖片轉二進位制流或byte

using System.IO; /// <summary> /// WebApi返回圖片 /// </summary> public HttpResponseMessage GetQrCode() {     var imgPath = @"D

ASP.NET MVC5+EF6+EasyUI 後臺管理系統(65)-MVC WebApi 用戶驗證 (1)

screen 屬性 access override jquery true mod 解析 action 系列目錄 前言: WebAPI主要開放數據給手機APP,其他需要得知數據的系統,或者軟件應用,所以移動端與系統的數據源往往是相通的。 Web 用戶的身份驗證,及

構建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x註入的後臺管理系統(66)-MVC WebApi 用戶驗證 (2)

簡單 權限管理 mark 調用接口 cat pps 總結 回顧 bsp 前言: 構建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x註入的後臺管理系統(65)-MVC WebApi 用戶驗證 (1) 回顧上一節,我們利用w

WebApi關於配置全局返回Json數據格式時間以及命名小寫

cors att spa prop json cas for IV con 1.直接在Global文件中配置: var formatters = GlobalConfiguration.Configuration.Formatters;var jsonFormatter

WebApi返回類型設置為json的三種方法

都是 eas Matter 配置 type edi types 分享 code web api寫api接口時默認返回的是把你的對象序列化後以XML形式返回,那麽怎樣才能讓其返回為json呢,下面就介紹兩種方法: 方法一:(改配置法) 找到Global.asax文件,在App

asp.net mvc5 設定Area下的為啟動頁

只需修改App_Start資料夾下RouteConfig中RegisterRoutes方法 1 public static void RegisterRoutes(RouteCollection routes) 2 { 3

ASP.NET MVC5+EF6+EasyUI 後臺管理系統-WebApi的用法與除錯

以往我們講了WebApi的基礎驗證,但是有新手經常來問我使用的方式 這次我們來分析一下程式碼的用法,以及除錯的方式 WebApi在一些場景我們會用到,比如: 1.對接各種客戶端(移動裝置) 2.構建常見的http微服務  3.開放資料  4.單點登陸  等...