【发布时间】:2014-02-02 02:33:39
【问题描述】:
我在一个解决方案中创建了 MVC 4 应用程序和 Web api 项目。从 mvc 4 应用程序我创建产品并上传相关图像,并从 web api 我将此产品模型发送给客户端。 有点像这个类:
class Product
{
GUID id;
string name;
string details;
string imageUrl;
}
为了在我的 MVC 4 应用程序中将图像保存到数据库,我这样做:
[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);
}
}
这是获取产品列表的 wep api 控制器:
public List<MenuComponent> Get()
{
return _productRepository.GetAll().ToList();
}
现在 web api 将所有产品发送到客户端
但是json中的image属性是绝对路径:
"imageUrl":"C:\\Users\\Hashem\\Documents\\Visual Studio 2012\\Projects\\MyApp\\Content\\Images\\image01.jpg
我希望图片链接是这样的:http://localhost:22012/Content/images/image01.jpg
我的客户端是一个 iOS 应用程序,将产品保存到数据库后,我将使用其图像属性下载所有图像。
那么我怎样才能得到正确的链接呢?
【问题讨论】:
-
你是如何创建 JSON 的?显示 web api 代码。
-
我已经添加了。但这没什么特别的。 web api 只是发送数据库中的所有属性而不更改它们。数据库中的 imagUrl 是我发布的。
-
好的,我想我有答案了。
标签: json image asp.net-mvc-4 asp.net-web-api