简体   繁体   中英

If-Modified-Since in Asp.Net Web Api 2

In my current project I'm using Angularjs with Asp.net Web Api 2 and trying to use some headers to determine workflow.

In one situation I want to update an entity only if "If-Modified-Since"-header is set and the value is the same as the stored entity.

My angular code looks like following:

$http.patch(selfLink, {message: item.message},
    {
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "If-Modified-Since": item.LastModified
        }
    }
)
.success(function(data, status, headers, config) {
    if (successCallback) {
        successCallback();
    }
})
.error(function(data, status, headers, config) {
    if (errorCallback) {
        errorCallback(data, status);
    }
});

The Web Api code looks like following:

public async Task<IHttpActionResult> Patch(int id, JObject updateEntity)
{
    using(var session = this.store.OpenAsyncSession())
    {
        var entity = await session.LoadAsync<TEntity>(id);
        if(entity == null)
        {
            return NotFound();
        }

        if(Request.Headers.IfModifiedSince.HasValue == false)
        {
            return BadRequest("Missing If-Modified-Since header");
        }

        // More code
    }

}

Every request I make I will get Request.Headers.IfModifiedSince.HasValue == false , but then I'm trying Request.Headers.GetValue("If-Modified-Since") I will get the value I'm asking for.

Does anyone know why the Request.Headers.IfModifiedSince property is not set?

Check the date format that you're sending as the header value. The date must be formatted according to the RFC 1123 standard. If

Request.Headers.GetValue("If-Modified-Since")

is not in this format, that would explain why

Request.Headers.IfModifiedSince

is null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM