网上电玩城捕鱼

网上电玩城捕鱼

飓风捕鱼电玩城拿出来分享一下

作者:apefpi 发布时间:2019-08-14 19:08

用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(); }

实质都能够改的,这样方便少许。

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