How return 304 status with FileResult in ASP.NET M

2019-03-15 06:02发布

As you may know we have got a new ActionResult called FileResult in RC1 version of ASP.NET MVC.

Using that, your action methods can return image to browser dynamically. Something like this:

public ActionResult DisplayPhoto(int id)
{
   Photo photo = GetPhotoFromDatabase(id);
   return File(photo.Content, photo.ContentType);
}

In the HTML code, we can use something like this:

<img src="http://mysite.com/controller/DisplayPhoto/657">

Since the image is returned dynamically, we need a way to cache the returned stream so that we don't need to read the image again from database. I guess we can do it with something like this, I'm not sure:

Response.StatusCode = 304;

This tells the browser that you already have the image in your cache. I just don't know what to return in my action method after setting StatusCode to 304. Should I return null or something?

3条回答
倾城 Initia
2楼-- · 2019-03-15 06:21

Don't use 304 with FileResult. From the spec:

The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.

It's not clear what you're trying to do from your question. The server doesn't know what the browser has in its cache. The browser decides that. If you're trying to tell the browser not to re-fetch the image when needed again if it already has a copy, set the response Cache-Control header.

If you need to return 304, use EmptyResult instead.

查看更多
成全新的幸福
3楼-- · 2019-03-15 06:23

In newer versions of MVC you'd be better off returning an HttpStatusCodeResult. That way you don't need to set the Response.StatusCode or mess with anything else.

public ActionResult DisplayPhoto(int id)
{
    //Your code to check your cache and get the image goes here 
    //...
    if (isChanged)
    {
         return File(photo.Content, photo.ContentType);
    }
    return new HttpStatusCodeResult(HttpStatusCode.NotModified);
}
查看更多
三岁会撩人
4楼-- · 2019-03-15 06:34

This blog answered the question for me; http://weblogs.asp.net/jeff/archive/2009/07/01/304-your-images-from-a-database.aspx

Basically, you need to read the request header, compare the last modified dates and return 304 if they match, otherwise return the image (with a 200 status) and set the cache headers appropriately.

Code snippet from the blog:

public ActionResult Image(int id)
{
    var image = _imageRepository.Get(id);
    if (image == null)
        throw new HttpException(404, "Image not found");
    if (!String.IsNullOrEmpty(Request.Headers["If-Modified-Since"]))
    {
        CultureInfo provider = CultureInfo.InvariantCulture;
        var lastMod = DateTime.ParseExact(Request.Headers["If-Modified-Since"], "r", provider).ToLocalTime();
        if (lastMod == image.TimeStamp.AddMilliseconds(-image.TimeStamp.Millisecond))
        {
            Response.StatusCode = 304;
            Response.StatusDescription = "Not Modified";
            return Content(String.Empty);
        }
    }
    var stream = new MemoryStream(image.GetImage());
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetLastModified(image.TimeStamp);
    return File(stream, image.MimeType);
}
查看更多
登录 后发表回答