Click here to Skip to main content
15,867,453 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.
[
{
"Path":"D:\\Images\\1.jpg"
},
{
"Path":"D:\\Images\\2.jpg"
},
{
"Path":"D:\\Images\\3.jpg"
}
]


I have hosted by above api in subdomain like- http://api.xyz.com/api/images/img1
now I want to consume this api in main domain like- http://xyz.com
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/");
//HTTP GET
var responseTask = client.GetAsync("Test/Img1");
responseTask.Wait();

var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readTask = result.Content.ReadAsStringAsync();
readTask.Wait();
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)
{



}
Posted
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
 
Comments
Vincent Maverick Durano 1-Sep-19 15:16pm    
5ed

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