您现在的位置:澳门网上真人龙虎 > 菲律宾申博官方网址 > 裸体真人博彩用ASP.NET(WebForm模式)开发WebAPI的几个常用步骤

裸体真人博彩用ASP.NET(WebForm模式)开发WebAPI的几个常用步骤

2019-08-15 22:16

用ASP.NET写了不少API了,有些人可以觉得MVC或者WCF(WebService)更适合写WebAPI,不过WebForm单一、易部署,还是很不错的WebAPI开发选择,以下是幼我正在开发中时时运用的几个步骤,拿出来分享一下,妙手请多多辅导!

1.返回少许字符串(最基本的,注定用得着)

[csharp] view plaincopyprint?

/// <summary> /// 返回字符串 /// </summary> /// <param>本质</param> void ResponseString(string Content) { Response.ContentType = "text/json"; Response.Write(Content); Response.End(); } /// <summary> /// 返回字符串 /// </summary> /// <param>本质</param> void ResponseString(string Content) { Response.ContentType = "text/json"; Response.Write(Content); Response.End(); }

先指定响应本质类型为:text/json,或者application/json、text/xml等等,然后举行响应,之后强行终场响应,沸少许本质谬误。

2.将对象转换成JSON字符串(JSON相比风行少许,XML就未几说了)

[csharp] view plaincopyprint?

/// <summary> /// 将对象转换为JSON字符串 /// </summary> /// <param>须要转换的对象</param> /// <returns></returns> string TransToJSON(object obj) { JavaScriptSerializer json = new JavaScriptSerializer(); return json.Serialize(obj); } /// <summary> /// 将对象转换为JSON字符串 /// </summary> /// <param>须要转换的对象</param> /// <returns></returns> string TransToJSON(object obj) { JavaScriptSerializer json = new JavaScriptSerializer(); return json.Serialize(obj); }

当心,这个步骤的实现须要增多一个引用

[csharp] view plaincopyprint?using System.Web.Script.Serialization;  

using System.Web.Script.Serialization;

能够将对象转换为JSON字符串,基本上是自己写好的类的对象,太阳城真人真钱网址,数组也是能够支持的。

3.少许谬误象征(未免会遇到谬误,要举行少许处理)

[csharp] view plaincopyprint?

/// <summary> /// 谬误处理 /// </summary> /// <param>象征,0:未知,1:结果为空,2:体式谬误,3:数据库谬误,4:参数谬误,太阳城真人真钱网址,5:办事器谬误,6:系统谬误,7:过错法,8:处理谬误,9:高出范畴</param> void ReError(int flag) { switch (flag) { case 1: ResponseString("null"); break; case 2: ResponseString("Format_Error"); break; case 3: ResponseString("DB_Error"); break; case 4: ResponseString("Param_Error"); break; case 5: ResponseString("Server_Error"); break; case 6: ResponseString("System_Error"); break; case 7: ResponseString("Illegal"); break; case 8: ResponseString("Process_Error"); break; case 9: ResponseString("Out_Of_Range"); break; case 0: ResponseString("Unknown_Error"); break; default: break; } Response.End(); } /// <summary> /// 谬误处理 /// </summary> /// <param>象征,0:未知,1:结果为空,2:体式谬误,3:数据库谬误,4:参数谬误,澳门网上真人龙虎,5:办事器谬误,6:系统谬误,7:过错法,8:处理谬误,9:高出范畴</param> void ReError(int flag) { switch (flag) { case 1: ResponseString("null"); break; case 2: ResponseString("Format_Error"); break; case 3: ResponseString("DB_Error"); break; case 4: ResponseString("Param_Error"); break; case 5: ResponseString("Server_Error"); break; case 6: ResponseString("System_Error"); break; case 7: ResponseString("Illegal"); break; case 8: ResponseString("Process_Error"); break; case 9: ResponseString("Out_Of_Range"); break; case 0: ResponseString("Unknown_Error"); break; default: break; } Response.End(); }

本质都能够改的,这样便利少许。

好了,就这么多,十分单一,也十分实用!