ShopController.cs
public class ShopController : ApiControllerBase { [Route("api/v1/shop/watermark")] [HttpPost] public ApiResult watermark(dynamic value) { // watermark/shopname.txt ApiResult parseParam = Parse<WatermarkParam>(value); if (!string.IsNullOrEmpty(parseParam.error)) return parseParam; var request_id = parseParam.request_id; try { var param = (WatermarkParam)parseParam.response; string fileName=GetUploadPath($@"watermark\{param.watermarkfilename}"); FileHelper.CheckDirectory(fileName); //var bitmap = ImageTool.Base64StringToImage(param.watermarkbase64); //ImageTool.WriteImage(bitmap, fileName); byte[] buffer=Convert.FromBase64String(param.watermarkbase64); ImageTool.WriteFile(buffer, fileName); var ack1 = ApiResult.New("", "", request_id, ""); return ack1; } catch (Exception ex) { return ApiResult.New("error", ex.ToString(), request_id, ""); } } class WatermarkParam : ApiParamBase { public string watermarkfilename; public string watermarkbase64; } }View Code
ApiControllerBase.cs
public class ApiControllerBase : ApiController { public const string SecretKey = "1z56'uTU;L.XWtPrkYfn>CRJ2BAV7<$dy!Go=:h+j*cNl~@}{,"; public ApiResult Parse<T>(object value) where T : ApiParamBase, new() { try { var json = JsonConvert.SerializeObject(value); T param = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json); string sign = EncryptTool.md5($"{param.timestamp}{SecretKey}"); if (sign != param.sign) { return ApiResult.New("sign_error", "sign_error", ApiResult.NewGuid(), ""); } return ApiResult.New("", "", ApiResult.NewGuid(), param); } catch (Exception ex) { return ApiResult.New("param_error", "param_error", ApiResult.NewGuid(), ""); } } public string ToJson(object obj) { string json = JsonConvert.SerializeObject(obj); return json; } public string GetUploadPath(string path) { var savepath = System.Configuration.ConfigurationManager.AppSettings["savepath"]; var fileName = Path.Combine(savepath, path); return fileName; } }View Code
ApiParamBase.cs
public class ApiParamBase { public ulong timestamp; public string sign; }View Code
ApiResult.cs
public class ApiResult { public static string NewGuid() { return Guid.NewGuid().ToString(); } public static ApiResult New(string error, string message, string request_id, object response) { var ret = new ApiResult(error, message, request_id, response); return ret; } public ApiResult(string error, string message, string request_id, object response) { this.error = error; this.message = message; this.request_id = request_id; this.response = response; } /// <summary> /// 对应错误编码 /// </summary> public string error { get; set; } /// <summary> /// 错误详细信息 /// </summary> public string message { get; set; } /// <summary> /// 请求ID,用GUID去掉-表示 /// </summary> public string request_id { get; set; } /// <summary> /// 响应内容 /// </summary> public object response { get; set; } }View Code
参考:
http://www.wisestudy.cn/article/webapi_post_put_delete.html
https://zhuanlan.zhihu.com/p/31279568?utm_id=0
https://blog.csdn.net/u013934107/article/details/136104537
https://blog.csdn.net/zgscwxd/article/details/133823295
https://blog.csdn.net/m0_46635910/article/details/128322556
https://blog.csdn.net/qq_43544461/article/details/130762168
https://www.cnblogs.com/landeanfen/p/5337072.html#_label1_0
标签:webapi,string,ApiResult,param,id,error,public,路由 From: https://www.cnblogs.com/zhaotiantian/p/18143740