Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Controllers
Models
CsvFormatter.cs
ExceptionHelper.cs
HashFactory.cs
Link.cs
Program.cs
README.md
Startup.cs
TodoApi.csproj
appsettings.Development.json
appsettings.json

README.md

Steps

using TodoApi.Models;
using System.Security.Cryptography;
using System.Text;

namespace TodoApi
{
    public static class HashFactory
    {
        public static string GetHash(TodoItem item)
        {
            if (item == null)
            {
                return string.Empty;
            }
            var itemText = $"{item.Id}|{item.IsComplete}|{item.Name}";
            using (var md5 = MD5.Create())
            {
                byte[] retVal = md5.ComputeHash(Encoding.Unicode.GetBytes(itemText));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }
    }
}
const string ETAG_HEADER = "ETag";
const string MATCH_HEADER = "If-Match";

Get:

var eTag = HashFactory.GetHash(item);
HttpContext.Response.Headers.Add(ETAG_HEADER, eTag);

if (HttpContext.Request.Headers.ContainsKey(MATCH_HEADER) &&
    HttpContext.Request.Headers[MATCH_HEADER].Contains(eTag))
{
    return new StatusCodeResult(304);
}
return new ObjectResult(item);

Put:

 var dbTag = HashFactory.GetHash(todo);
if (!HttpContext.Request.Headers.ContainsKey(MATCH_HEADER) ||
    !HttpContext.Request.Headers[MATCH_HEADER].Contains(dbTag))
    {
        return new StatusCodeResult(412);
    }
You can’t perform that action at this time.