简体   繁体   中英

How return a dictionary from WebAPI?

I have a MVC project that, from a view upload one or more files to a server, then , when i click submit a provate web service is recalled and i pass a collection of files to upload. The upload workings fine, but form webapi i have an object (class Store) that i would return.

I do not know if I am wrong to return the "raw" object from the web api or wrong to read it.

this is my model

    public class Store
    {
        public IDictionary<string, string> dictionaryOfStores = new Dictionary<string, string>();
        public string CodeText { get; set; }
    }

this i have my web api method:

public async Task<ActionResult<Store>> UploadAsync()
        {
            dictStores = new Store();
            foreach (var formFile in form.Files)
                    {

                        dictStores.dictionaryOfStores.Add(formFile.FileName, codefilename);
                        // also CodeText is filled here
                    }
                }
            }

            // return dictionary
            //

            return dictStores;
        }

this is my controller of mvc that recall private webservice

  public async Task<IActionResult> Upload(List<IFormFile> files)
        {

                    using (var client = new HttpClient())
                    {

                            var model = new FilesViewModel();
                            MultipartFormDataContent multiContent = new MultipartFormDataContent();
                            foreach (var formfile in files)
                            {
                                client.BaseAddress = new Uri(BaseAddress);
                                byte[] data;
                                using (var br = new BinaryReader(formfile.OpenReadStream()))
                                    data = br.ReadBytes((int)formfile.OpenReadStream().Length);
                                ByteArrayContent bytes = new ByteArrayContent(data);

                                multiContent.Add(bytes, "file", formfile.FileName);
                                model.Files.Add(new FileDetails { Name = formfile.GetFilename() });
                            }
                            var result = await client.PostAsync("api/Load/Upload", multiContent); // 
                            // here i have try to read result.Content but doesn't have my data
                            var res = await result.Content.ReadAsStreamAsync();
                            // here i have to put res in my model
                            return View("Summary", model);

                    }

im use asp net core 2.2 Thanks for the help

Maybe something like this

IDictionary<MyClass, int> resource = new Dictionary<MyClass, int> 
{
  { new Class {Property1="1"}, 1 },
  { new Class {Property1="11"}, 1 }
};

return Ok(resource); 

我通过看到答案解决了: 如何使用Httpclient获取对象和Web Api中的响应确定我能够读取数据:

await result.Content.ReadAsAsync<Store>()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM