I have created and MVC 4 app and web api projects in one solution. from mvc 4 app I create products and upload related images, and from web api I send this product model to the client. somehow like this class:
class Product
{
GUID id;
string name;
string details;
string imageUrl;
}
for saving image to database in my MVC 4 app I do this way:
[HttpPost]
public ActionResult Edit(Product product, HttpPostedFileBase image)
{
if (ModelState.IsValid)
{
if (image != null)
{
string imageName = image.FileName;
string location = Path.Combine(Server.MapPath("~/Content/Images/") , imageName);
image.SaveAs(location);
product.Image= location;
}
menuItemRepository.SaveOrUpdate(product);
TempData["message"] = string.Format("{0} has been saved", product.Name);
return RedirectToAction("Index");
}
else
{
// there is something wrong with the data values
return View(product);
}
}
this is wep api controller for getting products list:
public List<MenuComponent> Get()
{
return _productRepository.GetAll().ToList();
}
now web api sends all the products to the client
but image property in the json are absolute path:
"imageUrl":"C:\\Users\\Hashem\\Documents\\Visual Studio 2012\\Projects\\MyApp\\Content\\Images\\image01.jpg
I want the image link be something like that: http://localhost:22012/Content/images/image01.jpg
my client is an iOS app and after saving products to database I will download all the images using their image property.
so how can I get correct link?