webApi之FromUri和FromBody區別


ublic  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類的定義

1
2
3
4
5
6
7
public  class  FileRequest
  {
      public  string  name {  get set ; }
 
      public  string  SelectedItemId {  get set ; }
      public  IEnumerable<SelectListItem> Items {  get set ; }
  }

  

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

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

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

 

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

而非URL中獲取參數~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM