Can an ASP.NET MVC controller return an Image?

前端 未结 19 1642
旧时难觅i
旧时难觅i 2020-11-22 02:10

Can I create a Controller that simply returns an image asset?

I would like to route this logic through a controller, whenever a URL such as the following is requeste

19条回答
  •  醉话见心
    2020-11-22 02:41

    Yes you can return Image

    public ActionResult GetImage(string imageFileName)
    {
        var path = Path.Combine(Server.MapPath("/Images"), imageFileName + ".jpg"); 
        return base.File(path, "image/jpeg");
    }
    

    (Please don't forget to mark this as answer)

提交回复
热议问题