Environment:
Windows 2008, vs 2008 SP1, ASP. net mvc 1.0
------------------------------------------------------------------------------
1. ASP. net mvc applicationProgramDisable sessionstate
In ASP. net MVC: in the article on tempdata mechanism, we briefly discuss ASP. the implementation mechanism of tempdata in. Net MVC. By default, the dataprovider of tempdata is implemented as session-based storage.
Using session as the default data storage method is reasonable, but sometimes we do not allow session usage on our website. We need to disable this guy, if at this time you are simply on the web. is it OK if sessionstate is disabled in Config? Of course not!
Let's take a look at the situation:
Add <sessionstate mode = "off"/> to Web. config and run the program. An exception is reported immediately on the page:
The reason is very simple. According to the analysis above, every time an action is executed, it will load and save tempdata. If session is disabled, the above exception will inevitably occur.
What should we do now? Actually, I don't even want to use tempdata in my application. I just want to disable sessionstate. However, do I need to implement another set of tempdataprovider for this? Is the cost too high? As I said just now, I don't even want to use tempdata, so why don't I simply implement tempdataprovider that doesn't do anything.
Ii. Customize a nulltempdataprovider
Well, now I want to write a tempdataprovider that does nothing:
As you can see, nulltempdataprovider does not do anything. That is to say, if you write a statement like tempdata ["name"] = "Tristan", it is equivalent to a white Write statement, I don't need tempdata, so it is acceptable.
Well, the alternative is well written. Now we need to consider how to replace the default sessiontempdataprovider.
3. Global Application nulltempdataprovider
Method 1. Create a controller base class
Refer to the previous article to learn that tempdataprovider is in system. web. MVC. as defined in controller, it is easy to think of creating a base class for all our own controllers to specify the default tempdataprovider.
In the constructor of mycontrollerbase, specify tempdataprovider as an instance of nulltempdataprovider. Then let all our other controllers inherit from mycontrollerbase. Well, after running it, we found that the exception was gone, and it went quite well.
However, introducing a new controller base class is costly. If I have written n controllers in advance, it will be replaced, can you easily control the volume somewhere?
Method 2. Create controllfactory
ASP. net mvc is designed to be easy to expand. We just need to move a little bit in the Controller. So we thought of working hard on controllerfactory. Why not define a controllerfactory to set tempdataprovider:
Mycontrollerfactory inherits from defaultcontrollerfactory, override the createcontroller () method, and adds a value to tempdataprovider in this method. The next thing to do is simply register the controllerfactory in global. ascx:
The running result is the same as expected. sessionstate is disabled and nulltempdataprovider is implemented. You must note that tempdata cannot be used.