asp.net In the project , Use .ashx The file of ( General handler ) It can be used to handle requests from clients , And return the result of the server to the client . The type it can return can be text 、 Or pictures . occasionally , We can use... In projects .cs To do the same thing . however , Go directly to the browser .cs Your files will be ruthlessly rejected . You need to be in Web.Config You can configure it in OK 了 .

Before I start writing this blog , Read other materials . I found that this knowledge point is far from what I can write now . But I think , There must be a process in learning , Now that I know that in the process , Then write it down . When we have a deeper understanding , Go record the deeper things . in addition , The verification code generation example used in this paper , Reference resources Insus.Net A blog of Bo you (Insus.Net), Thank you , Read his blog and learn a lot .

First, create a .ashx The general procedure for ending (CreateVerifyCodeHandler.ashx) And a .cs At the end of the class file (CreateVerifyCode.cs). Create another Html file , Used to show captcha .Refresh The script function is placed in RealTime.js In the script file , Used to refresh captcha image .html The code is as follows :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="js/RealTime.js"></script>
</head>
<body>
<p> from .ashx Get the verification code from the file </p>
<img id="ashxValidateCodeImg" src="/Handlers/CreateVerifyCodeHandler.ashx" onclick="Refresh('ashxValidateCodeImg')" />
<hr />
<p> from .cs Get the verification code from the file </p>
<img id="csValidateCodeImg" src="/Handlers/CreateVerifyCode.ashx" onclick="Refresh('csValidateCodeImg')" />
</body>
</html>

I want to see others CreateVerifyCodeHandler.ashx Code for .

   public class CreateVerifyCodeHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
using (Bitmap b = new Bitmap(, , PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(b))
{
Rectangle rect = new Rectangle(, , , );
g.FillRectangle(Brushes.White, rect); string drawString = RandomCharacters.GenerateRandomString(); Font drawFont = new Font("Arial", , FontStyle.Italic | FontStyle.Strikeout);
using (SolidBrush drawBrush = new SolidBrush(Color.Black))
{
PointF drawPoint = new PointF(, );
g.DrawRectangle(new Pen(Color.Red, ), rect);
g.DrawString(drawString, drawFont, drawBrush, drawPoint);
}
b.Save(context.Response.OutputStream, ImageFormat.Jpeg);
context.Response.ContentType = "image/jpeg";
context.Response.End();
} }
} public bool IsReusable
{
get
{
return false;
}
}
}

here , We can browse the general handler directly in the browser , Because it returns a picture file . Pictured :

open CreateVerifyCodeHandler.ashx file , I found that there is no difference between the code inside and the code inside the ordinary class program . Can you do the same thing with a common class file ? therefore , Let's copy the code to CreateVerifyCode.cs In file . In the same way , Try browsing this in your browser .cs file , The results you see are really like this .

Next , open web.config file , find <httpHandler> section , Add the following configuration .

  <add verb="*" path="CreateVerifyCode.ashx" type="WebApplication1.Handlers.CreateVerifyCode" validate="false"/>

Browse... Again CreateVerifyCode file , But it's time to pay attention , We need to change the file name to CreateVerifyCode.ashx To visit . Otherwise, you can't browse directly .cs Final document . thus , We can also view the original creation directly in the browser Html file , Click on the image can also refresh the verification code .

ashx General procedures and HttpHandler More articles about

  1. ashx It is used in general processing program Session

    In the project , call ashx General processing procedures to obtain administrative divisions Json data , stay ashx It's useful for Session, But you can't get Session . Looking up the data, we learned that ashx The general processing program should use Session, have to ...

  2. ashx The general purpose of the handler file is

    Today, I went to the blog park , Accidentally found a good article , About ashx The use of documents . Article 1 : Send to server josn character string , Server side parsing This article is reprinted. :http://www.cnblogs.com/yzenet/p/3470388. ...

  3. ASP.NET ASHX General handler tutorial

    You don't want to create a normal ASP.NET Of Web Form page . And to return a dynamic picture through a query string .XML Or not HTML Webpage . This is a use C# The use of programming languages ASHX( General handler ) A simple tutorial for . brief introduction First , ...

  4. ashx General handler

    explain :     Although the handler can be created in a standard way , But the implementation steps are complex , In order to facilitate the application of processing program in website development , from Asp.net 2.0 Start ,asp.net A handler called a general handler is provided , Allow us to use ...

  5. .net General handler (httphandler) Realize file download function

    Handler The document code is as follows : public class MDMExporterWeb : IHttpHandler { public void ProcessRequest(HttpContext c ...

  6. asp.net ashx General handler Use async await Asynchronous direct copy Available

    I used to be lazy   meet ashx Use await Asynchrony bypasses   use aspx   perhaps mvc Asynchronous controller   This time the company needs   I checked the articles in China, but they are basically not simple copy To deal with a bunch of wrong key functions in the process BeginProcess ...

  7. Ajax request ashx General procedures to achieve file download

    The specific functions are , Select a line in the file data list , Click the download button above the form , Download the file . The file path is not included in the table , You can only get... In the database table ID, So the concrete implementation is like this : First click on a line , Get a line of data that you click on ID, use Ajax Pass on ...

  8. Use ashx General handler , Cannot read Session The problem of

    It's not used in general processing program files Session Of , It has to be done Session Interface can be used . public class RandomCode : IHttpHandler, System.Web.Sessi ...

  9. AJAX Submitted to the Handler.ashx The general handler returns json data ( String splicing )

    <%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...

Random recommendation

  1. Linux There are three ways to call the system

    system call (System Call) The operating system is a process and hardware device running in user mode ( Such as CPU. disk . Printers, etc ) A set of interfaces for interaction . When a user process needs a system call ,CPU Switch to kernel state through soft interrupt and start to execute kernel system ...

  2. thinkjs——session

    I met again in my work session problem , So I'll sort it out thinkjs Medium session Use small methods : Websites that require users to log in are basically inseparable from Session,ThinkJS By default, it supports multiple types of Session, ...

  3. c# In, the base class variable points to the instantiation of the derived class object

    This article is reprinted from :http://www.xuebuyuan.com/390279.html I verified this article one by one , It's exactly like this , And I've learned a lot , I think it's very useful , I hope it can help you . 1. ...

  4. use Navicat Write mysql The cursor of

    A thousand words can't compare with a simple and direct example : CREATE PROCEDURE pro_users() begin DECLARE myid int; DECLARE no int; ); ); ); ...

  5. [HDOJ5783]Divide the Sequence( greedy )

    Topic link :http://acm.hdu.edu.cn/showproblem.php?pid=5783 The question : to n Number , It is required to be divided into multiple segments , So that any prefix sum of each segment is not less than 0. From the back to the front , It won't affect ...

  6. hdu 4027 Can you answer these queries?

    Topic linking http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...

  7. Data Flow -&gt;&gt; DQS Cleansing

    Data Quality Services(DQS) yes SQL Server 2012 One of the features introduced . The task of this service is to standardize the client data and clean up the wrong data . For example, the client data is easy because the user outputs such as city name ...

  8. C# Keyword collation in

    C# All keywords in can be roughly divided into two parts: reserved keywords and context keywords Let's see if we don't know anyone Welcome to add Reserved keyword abstract  as  base  bool  break byte  case  ...

  9. shell Generate MAC Address

    # cat /dev/urandom |od -x |awk '{print $2,$3,$4}' |head -n 1 |sed -e 's/[[:space:]]//g' -e 's/\(..\) ...

  10. solve nginx+uWSGI Deploy Django When I met static file 404 The problem of

    Yesterday was the use of Django Self contained runserver Deployed servers , But because of runserver It's not stable , So it was decided to adopt uWSGI+nginx Deployment . It was installed yesterday uwsgi and nginx, Use this command to open 8 ...