IE6 is an old-fashioned browser. It does not support most of the new features released over the past eight years, but the browser still accounts for about 15-25% users, this severely restricts the development of browser technology. Many people think that it is time to abandon this old web browser. But now, it is more than just individuals and some non-profit organizations, several companies have even launched a website that appeals to people to discard IE6.
This "Evil IE6" is enough for me. Hey hey (Google decided to abandon IE6), so I decided to abandon IE6, which is a headache for me, who made me an activist.
In ASP. in net MVC2, disabling access by IE6 is actually very simple. We only need to get the IE version of the client browser and then judge whether the version is 6 or not, if it is 6, we will output a prepared page to prompt the user to change to another browser.
At the beginning, I wanted to use httpmodule for implementation, but I didn't understand it for a long time. So I decided to give up this solution and use actionfilter for implementation. (If you know how to do this, please don't hesitate to give me some advice ).
Actionfilter can filter actions. In actionfilter, we can disable access by IE6. However, we all know that actionfilter can only be applied to the Controller or action level and has no application.ProgramLevel filter, I heard that mvc3 will support application-level filters. Whether it is true or not, let's first look at how to implement it in MVC2.
First, we need to customize an actionfilter named checkpolicerattribute.
Public class checkpolicerattribute: actionfilterattribute
{
Public override Void Onactionexecuting (actionexecutingcontext filtercontext)
{
If (Filtercontext. httpcontext. Request. browser. majorversion = 6 )
{
Viewresult result = New Viewresult ();
Result. viewname = " Assumererror " ;
Filtercontext. Result = Result;
}
}
}
This actionfilter is executed on the action to determine whether ie on the client is IE6. If it is IE6, the content in the assumererror part of the view is output.
Next let's take a look at the content in the assumererror.
<% @ Control Language = " C # " Inherits = " System. Web. MVC. viewusercontrol <dynamic> " %>
< H3 > A letter to XXX users < / H3>
< Div >
< P >
Dear user, when you saw this letter, we just made a very difficult decision.
< / P>
< P >
We decided to reject Internet Explorer 6 users from accessing our website before the evil Internet Explorer 6 stops rampant abuse of users and website developers.
< / P>
< P >
Please switch to another browser for better browsing effect. We are sorry for the inconvenience!
< / P>
< P >
Finally, we give a piece of advice to the majority of users, "true love for life, stay away from IE6 "!
< / P>
< / Div>
In order that the filter can play a role in the entire application, we need to define a controller. This controller inherits from the System Controller and then adds the custom filter to the Controller, don't talk nonsense.Code.
[Checkexplorer]
Public class mycontroller: Controller
{
}
At last, every controller we created claimed to be the self-defined mycontroller. In this way, no matter which page the user accesses, he will see the custom letter and continue with the code.
Public Class Homecontroller: mycontroller
{
//
// Get:/home/
Public Actionresult index ()
{
Return View ();
}< P>
}
OK, you are done.