webApi之FromUri和FromBody區別

     public Link GetLink([FromUri] FileRequest fileRequest)
        {
            if (ModelState.IsValid)
            {
                var xml = WebConfigurationManager.AppSettings["appDiscoveryXml"];
                var wopiServer = WebConfigurationManager.AppSettings["appWopiServer"];
                bool updateEnabled = false;
                bool.TryParse(WebConfigurationManager.AppSettings["updateEnabled"], out updateEnabled);
                WopiAppHelper wopiHelper = new WopiAppHelper(HostingEnvironment.MapPath(xml), updateEnabled);

                var result = wopiHelper.GetDocumentLink(wopiServer + fileRequest.name);

                var rv = new Link
                {
                    Url = result
                };
                return rv;
            }

            throw new ApplicationException("Invalid ModelState");
        }

 

  FileRequest類的定義app

   public class FileRequest
    {
        public string name { get; set; }

        public string SelectedItemId { get; set; }
        public IEnumerable<SelectListItem> Items { get; set; }
    }

  

 這裏的FromUri強制從url中讀取FileRequest 對象,也就是說ui

    當訪問Http://localhost:80?name=eric&SelectedItemId={Guid.NewGuid()}url

   此處將自動轉換Uri中的參數做爲對象屬性忽略form傳遞的數據spa

 

而使用FromBody將強制從FormData中讀取數據orm

而非URL中獲取參數~xml

相關文章
相關標籤/搜索