我可以创建一个简单地返回图像资产的控制器吗?
我想通过控制器路由这个逻辑,每当请求如下URL时:
www.mywebsite.com/resource/image/topbanner
控制器将查找topbanner.png并将图像直接发送回客户端。
我见过这样的例子,你必须创建一个视图-我不想使用视图。我想只用控制器来做。
这可能吗?
我可以创建一个简单地返回图像资产的控制器吗?
我想通过控制器路由这个逻辑,每当请求如下URL时:
www.mywebsite.com/resource/image/topbanner
控制器将查找topbanner.png并将图像直接发送回客户端。
我见过这样的例子,你必须创建一个视图-我不想使用视图。我想只用控制器来做。
这可能吗?
当前回答
解决方案1:从图像URL在视图中呈现图像
你可以创建自己的扩展方法:
public static MvcHtmlString Image(this HtmlHelper helper,string imageUrl)
{
string tag = "<img src='{0}'/>";
tag = string.Format(tag,imageUrl);
return MvcHtmlString.Create(tag);
}
然后这样使用它:
@Html.Image(@Model.ImagePath);
解决方案2:从数据库中渲染图像
创建一个返回图像数据的控制器方法,如下所示
public sealed class ImageController : Controller
{
public ActionResult View(string id)
{
var image = _images.LoadImage(id); //Pull image from the database.
if (image == null)
return HttpNotFound();
return File(image.Data, image.Mime);
}
}
并在视图中使用它:
@ { Html.RenderAction("View","Image",new {id=@Model.ImageId})}
要在任何HTML中使用此actionresult渲染的图像,请使用
<img src="http://something.com/image/view?id={imageid}>
其他回答
您可以创建自己的扩展,并这样做。
public static class ImageResultHelper
{
public static string Image<T>(this HtmlHelper helper, Expression<Action<T>> action, int width, int height)
where T : Controller
{
return ImageResultHelper.Image<T>(helper, action, width, height, "");
}
public static string Image<T>(this HtmlHelper helper, Expression<Action<T>> action, int width, int height, string alt)
where T : Controller
{
var expression = action.Body as MethodCallExpression;
string actionMethodName = string.Empty;
if (expression != null)
{
actionMethodName = expression.Method.Name;
}
string url = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection).Action(actionMethodName, typeof(T).Name.Remove(typeof(T).Name.IndexOf("Controller"))).ToString();
//string url = LinkBuilder.BuildUrlFromExpression<T>(helper.ViewContext.RequestContext, helper.RouteCollection, action);
return string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" alt=\"{3}\" />", url, width, height, alt);
}
}
public class ImageResult : ActionResult
{
public ImageResult() { }
public Image Image { get; set; }
public ImageFormat ImageFormat { get; set; }
public override void ExecuteResult(ControllerContext context)
{
// verify properties
if (Image == null)
{
throw new ArgumentNullException("Image");
}
if (ImageFormat == null)
{
throw new ArgumentNullException("ImageFormat");
}
// output
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = GetMimeType(ImageFormat);
Image.Save(context.HttpContext.Response.OutputStream, ImageFormat);
}
private static string GetMimeType(ImageFormat imageFormat)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
return codecs.First(codec => codec.FormatID == imageFormat.Guid).MimeType;
}
}
public ActionResult Index()
{
return new ImageResult { Image = image, ImageFormat = ImageFormat.Jpeg };
}
<%=Html.Image<CapchaController>(c => c.Index(), 120, 30, "Current time")%>
更新:有比我原来的答案更好的选择。这在MVC之外工作得很好,但最好坚持使用返回图像内容的内置方法。见赞成投票的答案。
你当然可以。试试下面这些步骤:
将映像从磁盘加载到字节数组中 缓存图像,如果您希望对图像有更多的请求,并且不需要磁盘I/O(我的示例在下面没有缓存它) 通过响应改变mime类型。ContentType 响应。写出图像字节数组
下面是一些示例代码:
string pathToFile = @"C:\Documents and Settings\some_path.jpg";
byte[] imageData = File.ReadAllBytes(pathToFile);
Response.ContentType = "image/jpg";
Response.BinaryWrite(imageData);
希望有帮助!
这对我很管用。 因为我将图像存储在SQL Server数据库上。
[HttpGet("/image/{uuid}")]
public IActionResult GetImageFile(string uuid) {
ActionResult actionResult = new NotFoundResult();
var fileImage = _db.ImageFiles.Find(uuid);
if (fileImage != null) {
actionResult = new FileContentResult(fileImage.Data,
fileImage.ContentType);
}
return actionResult;
}
在上面的代码片段中,_db.ImageFiles.Find(uuid)正在db (EF上下文)中搜索图像文件记录。它返回一个FileImage对象,它只是一个我为模型制作的自定义类,然后将其用作FileContentResult。
public class FileImage {
public string Uuid { get; set; }
public byte[] Data { get; set; }
public string ContentType { get; set; }
}
解决方案1:从图像URL在视图中呈现图像
你可以创建自己的扩展方法:
public static MvcHtmlString Image(this HtmlHelper helper,string imageUrl)
{
string tag = "<img src='{0}'/>";
tag = string.Format(tag,imageUrl);
return MvcHtmlString.Create(tag);
}
然后这样使用它:
@Html.Image(@Model.ImagePath);
解决方案2:从数据库中渲染图像
创建一个返回图像数据的控制器方法,如下所示
public sealed class ImageController : Controller
{
public ActionResult View(string id)
{
var image = _images.LoadImage(id); //Pull image from the database.
if (image == null)
return HttpNotFound();
return File(image.Data, image.Mime);
}
}
并在视图中使用它:
@ { Html.RenderAction("View","Image",new {id=@Model.ImageId})}
要在任何HTML中使用此actionresult渲染的图像,请使用
<img src="http://something.com/image/view?id={imageid}>
我有两个选择:
1)实现你自己的IViewEngine,设置控制器的ViewEngine属性,你正在使用你的ImageViewEngine在你想要的“image”方法。
2)使用视图:-)。只需改变内容类型等。