Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create an ASP.NET MVC controller that returns an Image

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 206
    Comment on it

    It is a common requirement where we need to create a controller that simply returns an image. Following is one of the possible solutions for implementing the above functionality.

    We need to add the following code on controller page

    public ActionResult Image(string imageId)
    {
        try
        {
            string directory = Server.MapPath("/Image");
           string imagePath = Path.Combine(directory , imageId + ".jpg");
            return base.FileimagePath , "image/jpeg");
        }
        catch(Exception ex)
        {
          throw;
        }
    }

    /Image : It is the path where images are located

    id : It is the identifier of the image

    The URL of the path is : http://localhost/MyController/Image/MyImage

    On accessing the above url the image with the requested identifier gets served.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: