Web API返回自定义数据给客户端

服务端出现异常时,返回给客户端status仍然是ok的。因此在前端的catch或是error是得到不到服务端的throw异常信息的。

所以,你在服务端中,把异常时,也得作为成功执行返回给客户端。


你可以写一个类别:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Insus.NET.APIs
{
    public class Response
    {
        public bool ResponseStatus { get; set; }
        public string Message { get; set; }
        public object ResponseData { get; set; }

        public Response() { }

        public Response(bool status, string message, object data)
        {
            ResponseStatus = status;
            Message = message;
            ResponseData = data;
        }
    }
}
Source Code

 

Web API实现数据返回给客户端。

 

 [HttpPost]
        public async Task<IHttpActionResult> GenerateQrCode(JObject jObj)
        {   

            Response response;

            try
            {
                //WriteFile(qr, out f);

                BarCode bc = new BarCode()
                {
                    BackgroundColor = jsonParams.BackgroundColor,
                    ForegroundColor = jsonParams.ForegroundColor,
                    Width = jsonParams.Width,
                    Height = jsonParams.Height,
                    Content = jsonParams.Content,
                    FileName = f,
                    MineType = "image/png"
                };

                response = new Response();
                response.ResponseStatus = true;
                var list = new List<BarCode>() { bc };
                response.ResponseData = list.FirstOrDefault<BarCode>();
            }
            catch (Exception ex)
            {
                response = new Response()
                {
                    ResponseStatus = false,
                    Message = ex.Message                    
                };

            }

            return await Task.FromResult(Ok(response));
        }
Source Code

 

前端angularjs呼叫api:

 

 $http({
                method: 'POST',
                url: '/api/IoSvc/GenerateQrCode',
                dataType: 'json',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                data: JSON.stringify(arg),
            })
                .then(
                    function (response) {
                        var d = response.data
                        if (d.ResponseStatus) {
                            $scope.noImageHide = false;

                            $scope.FileName = d.ResponseData.FileName;
                        }
                        else {      
                            $scope.noImageHide = true;
                            $scope.FileName = "";
                            $window.alert(d.Message);
                        }
                    },
                    function (response) {
                        $window.alert(response.data);
                    }
                )
                .catch(function (response) {
                    $window.alert("catch:" + response.data);
                })
                //.finally(function () {
                //    console.log("Task Finished.");
                //})
                ;
Source Code

 

posted @ 2020-08-20 15:09  Insus.NET  阅读(629)  评论(0编辑  收藏  举报