Clearer MVC
In every application there are some variables that are set by the ASP.NET application( ASP.NET_SessionId cookie ) and some that are set by the programmer( cached data in Application/Session/Cache/Cookies and so on).
I wanted every time to have a page where I can “clear” / delete those – and not found. So it’s the Clearer project.
It consists of :
- ClearerController with 2 Actions: Index and DeleteItem
- 2 Views : Index.cshtml and EditAppData.cshtml
- Different Models:
- SourceData – enum – can be : None , Application , Cache , Session , Cookies
- AppData – maintains Key/Value and SourceData pairs
- ListAppData – loads data from Application , Cache , Session , Cookies – and deletes.
To make an example, I have put in the Application_Start and Session_Start different values. So the screen is the following:
What I learn from the code:
- The Cookies, Applications, Session , Cache items can be easily converted to an DictionaryEntry and the code can be like this:
DictionaryEntry de = new DictionaryEntry(item, sess[item.ToString()]); AddNew(de, SourceData.Session);
- Code must be error prone – what if some item in Session is null ? So , if I have the Key, all is good:
private void AddNew(DictionaryEntry de, SourceData sd) { AppData ap = new AppData() { source = sd, Key = de.Key.ToString() }; try { var obj = de.Value; ap.Value = (obj == null) ? Null : obj.ToString(); } catch (Exception ex) { ap.Value = string.Format(ErrorToString, ex.Message); } this.Add(ap); }
- The dog-food is good: I have followed my advice from msprogrammer.serviciipeweb… and it works ( used for Remove )
[HttpPost] public JsonResult DeleteItem(string TheKey, int Source) { try { var lad = new ListAppData(); lad.DeleteItem(TheKey, (SourceData)Source); return Json(new { ok = true, message = "" }); } catch (Exception ex) { return Json(new { ok = false, message = ex.Message }); } }
- When you pass strings in Javascript, there is a simple way to encode: HttpUtility.JavaScriptStringEncode
<a href="javascript:removeItem('@HttpUtility.JavaScriptStringEncode(Model.Key)','@((int)Model.source)','@id')">Remove</a>
Possible uses:
- For developers – when they want to see what happens when a cache item no longer exists
- For developers – to put to site admins some simple tool to reload data from Cache/Application . Just edit the LoadAll function to load only Cache/Application
- For developers – to test easily the session. Just delete ASP.NET_SessionId cookie – you will get another one when you refresh the page.
You can view online at http://clearer.apphb.com/Clearer
The project could be found at http://clearer.codeplex.com and have all – source code, downloadable project .
Next week it will be a Nuget item.
For more features , please leave me a comment here or on codeplex at issues
Nuget package at http://nuget.org/packages/Clearer