Some Supplements to "creation of verification codes"

Source: Internet
Author: User

The last time I wrote some code about the verification code, I learned some things in Drawing. I am very happy and happy to do some good deeds for everyone, so I came up with the idea of encapsulating the verification code into a simple control and releasing it. At first I used the practice of ashx (a general processing program, also known as Httphander), OK. In WebForm and MVC, there is no problem-the code is very simple. If you put ashx in the program root directory, you can get an img in the page:

In this way, the handler is automatically requested to generate the img Tag (do not forget to configure the handler ).
Then I want to create a dll, because I always feel that it is not very formal to release ashx (others all release dll, huh, huh), so I created a class library (ClassLibrary) myself ), create a corresponding class to implement IHttpHandler and IRequiresSessionState (copy the correct code in the original ashx, that is, all the code in the previous article ). Compile it into a dll component and configure it properly in  
<Configuration>
<System. web>
<! --- Httphandler's configuration here ...... -->
<HttpHandlers>
<Add path = "*. req" verb = "*" type = "TestLibrary. DrawImage"/>
</HttpHandlers>
..................
 
Then experiment at the same time in WebForm and MVC -- the result is strange: In WebForm, everything is normal, but in the MVC program, the result is that the Red Cross forks report an error (no picture shown, depressed ......)
Later, I asked a lot of questions. I didn't even realize it until I met imran_ku07 (see http://forums.asp.net/t/1800318.aspx)-when you wrote "/Image. in req, the default route will match according to "Controller/Action/DefaultOptionalValue". In this case, the Controller will become "Image. req ", naturally there is no such Controller, and then an error is reported naturally (Images cannot come out ).
The solution is to prevent this path from being parsed by default Route, and exclude this path (in bold) using IgnoreRoute ):
[C #]
 
Public class MvcApplication: System. Web. HttpApplication
{
Public static void RegisterRoutes (RouteCollection routes)
{
Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");
Routes. IgnoreRoute ("{resource}. req/{* pathInfo }");
Routes. MapRoute (
"Default", // route name
"{Controller}/{action}/{id}", // URL with Parameters
New {controller = "Default", action = "Index", id = UrlParameter. Optional} // Default value of the Parameter
);

}

Protected void Application_Start ()
{
AreaRegistration. RegisterAllAreas ();
RegisterRoutes (RouteTable. Routes );
}
}
 
[VB. NET]
 
Public Class MvcApplication
Inherits System. Web. HttpApplication
Public Shared Sub RegisterRoutes (routes As RouteCollection)
Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }")
Routes. IgnoreRoute ("{resource}. req/{* pathInfo }")
'Route name
'Url with Parameters
'Parameter Default Value
Routes. MapRoute ("Default", "{controller}/{action}/{id}", New {_
Key. controller = "Default ",_
Key. action = "Index ",_
Key. id = UrlParameter. [Optional] _
})

End Sub

Protected Sub Application_Start ()
AreaRegistration. RegisterAllAreas ()
RegisterRoutes (RouteTable. Routes)
End Sub
End Class
 
In this way, ASP mvc can also be used! God!
"Happiness, happiness, and sorrow" -- a software programmer asked me about how to block the MVC plug-in at a time in the afternoon (its project root directory contains the plug-in folder, there are a large number of other important files in the file, and you do not want to access them now ). I thought about the following and gave two solutions:
1) configure the following in the root directory web. config (the face folder and file cannot be accessed after the subdirectory -- plugin in the root directory)
<System. web>
<HttpHandlers>
<Add path = "/plugin/*" verb = "*" type = "System. Web. HttpNotFoundHandler"/>
</HttpHandlers>
..................
2) copy a web. config file to the plugin folder and configure it like this (indicating that all subfolders and files in the current folder cannot be accessed)
<System. web>
<HttpHandlers>
<Add path = "*" verb = "*" type = "System. Web. HttpNotFoundHandler"/>
</HttpHandlers>
..................
His problem is solved, and I came up with a new problem-since MVC can directly access the file in the form of "http: // localhost/folder name/file, so why does it not resolve "folder name" to Controller, and "file" to Action? I guess:
1) by default, resolution of "Hidden Rules" in any web application is to find the corresponding file (traditional ASP. NET Web is no exception), so MVC also has this potential rule-if the Virtual Path entered in the address bar can map to find the real file, then directly return the result, it will not be in the Route.
2) If you cannot find:
2.1) Try Route (according to the default or other defined rules: http: // localhost/{Controller}/{Action}/DefaultOptionalParameter) for parsing.
2.2) if the parsing is successful, the View corresponding to the Action is returned. The parsing fails and an exception is thrown.
3) If a request address (such as Image. req is a pure ashx request, and the actual file path does not exist). If you directly use the Route rule to match the request, it is definitely incorrect. In this case, you should inform the system that "this is an exception" (use IgnoreRoute ).



From Serviceboy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.