天天看點

web api(基于NFine架構) 中接口跳轉資料格式錯誤解決方案

using NFine.Code;
using NFine.Domain;
using System.Web.Http;
using Newtonsoft.Json;

namespace Api.Controllers.bp
{
    public class School_VersionController : BaseController
    {
        // GET: School_Version
        //3.3檢測是否有新的版本資訊
        [System.Web.Http.HttpPost]
        public IHttpActionResult checkVersion([FromBody]param param)
        {
            IInterFaceEntity iinter = new IInterFaceEntity();
            WebHelper webhelp = new WebHelper();
            string Url = "http://localhost:8082/Api/School_Version/checkVersion";
            bool isPost = true;
            try
            {
                string parament = "F_currentVersion=" + param.F_currentVersion + "";
                string getversion = webhelp.HttpWebRequest(Url, parament, isPost).Replace("\r\n", "");
                return Json(JsonConvert.DeserializeObject(getversion, iinter.GetType()));
            }
            catch (System.Exception ex)
            {
                
                iinter.IsError = true;
                iinter.ErrorCodeValue = "0001";
                iinter.ErrorMsgInfo = ex.Message;
                iinter.Body = "";
                return Json(iinter);
            }
        }
    }

    public class param
    {
        public string F_currentVersion { get; set; }//版本号
    }
}      

問題:當接口1調用接口2時,接口2傳回的時一個對象,而接口1中接受并解析完後,會存入一個字元串中,該字元串格式并不能符合json的格式要求,是以會出錯。

主要的一步:

return Json(JsonConvert.DeserializeObject(getversion, iinter.GetType()));

将字元串getversion再轉成要傳回的對象類型,再傳回,這樣就不會出錯了。      
web api(基于NFine架構) 中接口跳轉資料格式錯誤解決方案