MVC browser history

This is the part 5 of 5 of my implementing of a MVC Browser history

MVC browser history – idea

Browser history –2 – implementing, small bugs

Browser history 3–trying to Nuget – modifications in order to can be transformed from an application to a component

Browser history 4–NuGet again – finally Nuget deployment

Browser history–part 5–conclusions – conclusions

I want to do another small open source utilities project – MVC browser history . That seems simple at the beginning – but , yet , there are small tasks to do that are not so easy .

What does MVC browser history  does? Well, keep an history of the links the user clicks through an MVC site. I must eliminate the ajax requests and Child requests – so keep the just the URL’s

So what I have learned after one hour:

Thinking process :

I must somehow maintain the url’s – either in database , either in memory – so a Repository concept came handy.

Data to be saved includes :

  1. URL –easy –  HttpContext.Request.Url.ToString();
  2. Date – easy – DateTime.Now;
  3. UserName – not so easy – what if user not registered?
  4. PageName – it should belong to that, but, from a RelationalDatabase in a 3rd form, should be another table.

Named this class BrowserUserHistoryData ( rather a long name, but descriptive)

Now detailing:
URL – possible problem : differentiate between Person/Edit/1 and Person/Edit/2. It’s a feature, not a bug Winking smile : browser history, not Action history. More, the User can have acces to Person/Edit/1 and NOT Person/Edit/2.
UserName – maybe Session , if not registered? (con.User.Identity.IsAuthenticated ? con.User.Identity.Name : con.Session.SessionID); , if not WebApp, then … ok, not for this time.

How to obtain the data? The global filter from http://bradwilson.typepad.com/blog/2010/07/aspnet-mvc-filters-and-statefulness.html

What to obtain ? A user history and first 5 links order by count

Programming:

Errors repaired:

  1. Brad Wilson put data in context.HttpContext.Items . Forget that is good for one request only. Thinking about how to persist and obtain data in MVC action and Global filter- and storing to Application . Made public static T AddOrRetrieveFromApplication
  2. Adding ViewName in OnResultExecuting, not in OnResultExecuted.
  3. Reading data from Repository WITHOUT persisting there Winking smile.
  4. Thinking that the Model of the page is just the list of user history. Nope, it is also the first 5 links order by count : HistoryViewModel
    public class HistoryViewModel
        {
            public BrowserUserHistory UserHis;
            public IBrowserUserHistoryRepository rep;
        }
    

Errors not repaired:

  1. Not showing just HIS user browser history, but all ( not to be repaired, since Cassini changes session_id all the time) . NOT TO BE REPAIRED.
  2. Latest links, history himself, not show view name

image

What I have enjoyed:

  1. Programming process
  2. The idea that will be useful to other people
  3. Linq:  – can be translated to database also.
     public IEnumerable<KeyValuePair<string,int>> MostUsed(int Count, DateTime? date)
            {
                var data= historyMemory.Where(item =>(date==null || item.Date.Subtract(date.Value).Days == 0)).GroupBy(item=>item.Url).OrderByDescending(g=>g.Count()).Take(Count);
                return data.Select(i =>new KeyValuePair<string,int>( i.Key,i.Count()));
            }
    

What to do next:

  1. Correct the error
  2. Test more the application with a real application
  3. Make the application understandable – comments.
  4. Make the application publicly available
  5. Spread the word ( already did, but not enough).

Code can be downloaded from BrowserHistory

. Play with it – and, if you like, drop a comment.