Click here to Skip to main content
15,742,150 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have Web API that will return all image URL in Json Result.

I have hosted by above api in subdomain like-
now I want to consume this api in main domain like-
but image are not showing . please help...

What I have tried:

Below is the code that i am using to display an image in mvc application.

public ActionResult Index()
//IEnumerable<imagepath> students = null;
List<imagepath> ImgInfo = new List<imagepath>();
using (var client = new HttpClient())
client.BaseAddress = new Uri("http://localhost:39085/api/");
var responseTask = client.GetAsync("Test/Img1");

var result = responseTask.Result;
if (result.IsSuccessStatusCode)
var readTask = result.Content.ReadAsStringAsync();
ImgInfo = JsonConvert.DeserializeObject<list<imagepath>>(readTask.Result);

else //web api sent error response
//log response status here..

//students = Enumerable.Empty<imagepath>();
ImgInfo = null;
ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
return View(ImgInfo);

View Page code-
@model IEnumerable<mvctest.models.imagepath>
@foreach (var item in Model)

Updated 31-Aug-19 23:13pm

1 solution

I can't see the code you use to render the image, but I'm guessing that you create an img tag with as src the API response.

D:\Images1\1.jpg is not a file path that will be recognized by your browser. You'll have to use this: file:///D:/Images1/1.jpg

But note that if anyone accesses your MVC application from a computer that's not yours, the images will never render, because their browser will look at their local file system, and the images won't be there.
Share this answer
Vincent Maverick Durano 1-Sep-19 15:16pm    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900