Category: Code


I wrote this library so I don’t have to keep track if I should use HttpRuntime.Cache, AppDomain.CurrentDomain.SetData, HttpContext.Current.Session, or a Dictionary…now I just use GlobalCache or UserCache.  It also makes unit testing a webapp a snap, because if you use IGlobalCache/IUserCache in your code, you just inject the Windows-based cache in your unit test setup.

You can download the solution with dlls here: Universal Caching solution

A breaking change in ASP.NET 4.0 causes a javascript error to be thrown on a page if a query string has a “potentially unsafe” character, such as an apostrophe in a last name.

You’ll see the error (possibly only while debugging): “A potentially dangerous Request.QueryString value was detected from the client” when passing the special character in a query string in a button click or a jqgrid’s FillGrid postData BuildGrigArgs function.

The fix is easy:  set the behavior back to ASP.NET 2.0 in the web.config with this line in the system.web section right below your </compilation> tag (the requestPathInvalidCharacters parameter here blocks everything except a single quote):

 <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,:,&amp;,\" requestValidationMode="2.0" />

And in your pages be sure you don’t encode the parameter in the MVC code in your javascript (i.e. you’ll need to use <%= instead of <%: for those parameters that need special characters). 

For example, this:

    function BuildPreviousArgs() {

            var args = new Object(); 

            args.FilterLastName = "<%: Model.FilterLastName %>"; 

            return $.param(args); 

        } 

Has to be changed to this:

    function BuildPreviousArgs() {

            var args = new Object(); 

            args.FilterLastName = "<%= Model.FilterLastName %>"; 

            return $.param(args); 

        }

If you upgrade from MVC 1 to 2 or higher and your controller factory (such as StructureMapControllerFactory) throws a “no suitable method found to override” error for your GetControllerInstance method, be sure you have a reference to System.Web.Routing:

using System.Web.Routing;

And be sure you add the RequestContext parameter to the GetControllerInstance method call like this:

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)

How to install iPython in Console2 on Windows

Make sure python is referenced in your Path: < my python path >\python.exe (where < my python path > is the directory where you installed python, such as "C:\Python26").

Make sure IPython is installed (from here: http://ipython.scipy.org/moin/Download)

Make sure PyReadline is installed (from here: https://launchpad.net/pyreadline/+download)

In Console2, right-click go to Edit –> Settings… –> Tabs Add new tab with

 Title: iPython
 Icon: < my python path >\DLLs\py.ico
 Shell: cmd.exe /k "< my python path >\Scripts\ipython.py -p sh"
 Startup dir: C:\ (or wherever)

Hit ok and in your Console just go to File –> New Tab –> iPython and you’re off and running!

How to install Flask in your Python app

1. Create a directory for your project (e.g. “hello”). 2. In this directory, create a hello.py file with this code:

  from flask import Flask
  app = Flask(__name__)

  @app.route("/")
  def hello():
      return "Hello World!"

  if __name__ == "__main__":
      app.run()

3. Download and install latest setuptools for your version of python.
4. Download latest flask, werkzeug, and jinja2, and decompress each in their own directory in your hello project like this:

  hello
    flask
    jinja2
    werkzeug

Note: Make sure the files are decompressed so the setup.py is in the root of each folder.

5. In the cmd shell for your hello project, run

  easy_install Flask

6. Then

  python hello.py

7. if all is well, you should see

  * Running on http://127.0.0.1:5000/

8. Finally, you should see “Hello World!” in your browser at http://127.0.0.1:5000

© 2012 Robert Corvus