0

I am using Azure Redis cache with my key is the request url. I have a web-api2 / mvc5 application which returns the cached result from Redis server if key with request url exist there otherwise it will process the request and save the result in Azure Redis cache server. I have mobile clients for IOS/Android as well as javascript. The problem is that some time my url look like,

http://example.com/MyPath/?b=2&a=1&c=
http://example.com/MyPath/?b=2&a=1&c
http://example.com/MyPath/?a=1&b=2
http://example.com/MyPath/?c=&a=1&b=2
http://example.com/MyPath/?a=1&b=2
http://example.com/MyPath/?a=1&b=2&c=

The above url points to the same resource. It should return the same response(cached response if exist) but since key(url) is different it process the request completely and save a different record on Azure Redis cache server. How to solve this scenario?

Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322

2 Answers2

1

Using MVC routing will give you a standard format to your URL's.

You will be able to use the default html helpers with ASP.NET such as @Html.Action("MethodName","Controller",new { a=1, b=2 })

You can specify custom routing templates in your RouteConfig.cs class (In app_start by default) or you can use Attribute Routing.

MSDN: MVC5 Attribute Routing

Dan
  • 968
  • 1
  • 8
  • 21
1

Here is what I have came so fast, any improvement will be appreciated,

    private static string SanitizeUrl(string url)
    {
        var uri = new Uri(url);
        var path = uri.GetLeftPart(UriPartial.Path);
        path += path.EndsWith("/") ? "" : "/";
        var query = uri.ParseQueryString();
        var dict = new SortedDictionary<string, string>(query.AllKeys
            .Where(k => !string.IsNullOrWhiteSpace(query[k]))
            .ToDictionary(k => k, k => query[k]));
        return (path + ToQueryString(dict)).ToLower();
    }

    private static string ToQueryString(SortedDictionary<string, string> dict)
    {
        var items = new List<string>();
        foreach (var entry in dict)
        {
            items.Add(string.Concat(entry.Key, "=", Uri.EscapeUriString(entry.Value)));
        }
        return (items.Count > 0 ? "?" : "") + string.Join("&", items.ToArray());
    }
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322
  • FYI - HttpUtility.ParseQueryString("") returns a NameValueCollection that is actually a HttpValueCollection. Calling .ToString on the HttpValueCollection will return a formatted query string. Example -> http://pastebin.com/A0DwH9w5 – Jeremy Bell Apr 28 '15 at 14:26