【问题标题】:asp.net webApi and sending model with image property to to the clientasp.net webApi 并将具有图像属性的模型发送到客户端
【发布时间】: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


【解决方案1】:

发布图片后,您将其保存到实际位置

   string location = Path.Combine(Server.MapPath("~/Content/Images/") , imageName);

并在您的数据库中存储相同的绝对位置。相反,将图像保存到物理位置,但将相对位置存储在数据库中。

   string imageName = image.FileName;

   string location = Path.Combine(Server.MapPath("~/Content/Images/") , imageName);
   image.SaveAs(location);

   string rellocation = string.Format( "/Content/Images/{0}", imageName ); 
   product.Image = rellocation;

这样,JSON 将返回相对路径 /Content/Images/image01/jpg

如果您需要绝对服务器 uri,请使用

   string rellocation = Path.Combine( HttpRuntime.AppDomainAppVirtualPath, 
       string.Format( "/Content/Images/{0}", imageName ) );

这应该返回http://localhost:22012/Content/images/image01.jpg

编辑:如果由于某些原因这对您不起作用,请尝试

   Uri uri = this.Request.Url;
   String appDomainPath = uri.Scheme + "://" + uri.Authority;

   string rellocation = Path.Combine ( appDomainPath, ... );

即尝试从当前请求路径获取路径。这应该在 mvc 控制器中工作。

【讨论】:

  • 我有一个问题:正如我提到的,我有 2 个项目,一个用于在 mvc 4 中编辑产品,一个用于使用 web api 向客户端发送数据。 (为什么我有 2 个项目是因为我不能为 MVC 和 web api 拥有 一个依赖解析器)mvc 在其内容目录中上传图像,而 web api 需要访问 mvc 内容目录。这可能吗?因为图像位于 mvc 内容目录中!我该如何解决这个问题?
  • 这有关系吗?使用 mvc 发布图像,以便 mvc 应用程序正确保存 uri。
  • 我在一个解决方案中有两个项目,一个是 myApp.Web.UI,另一个是 myApp.Web.Api。两者都使用一个数据库。从 web.UI 我创建产品并上传他们的图像。所以图像将保存在 web.UI 内容目录中。和 web.api 将数据库中的产品信息发送给客户端。如果在将产品发送到客户端链接之前将 web.api appdomain 添加到图像位置,则类似于:myappWebApi/contnet/images/image01.jpg。但是这个目录中没有图像,因为 webUi 已经将图像上传到它的项目图像目录中!
  • 您不会将 webapi 域添加到路径中。上面的代码仅在图像发布时在您的 mvc 控制器中运行。是你写的,它在你的 mvc 应用程序中运行。如果您的 mvc 应用程序将完整路径写入图像,包括 mvc 根路径,那么如果将完整路径写入数据库,webapi 将如何更改呢?
  • 我尝试将带有绝对 url 路径的图像路径写入数据库。它将解决问题。但是 HttpRuntime.AppDomainAppVirtualPath jsut 只给了我“/”。而已!为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 2013-10-08
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多