博客 (145)

string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
//Color[] color ={Color.White, Color.Yellow, Color.LightBlue, Color.LightGreen,
//                Color.Orange, Color.LightCyan, Color.LightPink, 
//                Color.LightSalmon };
Color[] color = {Color.Black, Color.Blue, Color.Red, Color.DarkViolet, 
                 Color.Chocolate, Color.DarkGreen, Color.DeepSkyBlue, 
                 Color.DarkCyan };
//字体列表,用于验证码 
string[] font = {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh",
                 "PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符 
char[] character ={'2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E',
                   'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T',
                   'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串 
for (int i = 0; i < 4; i++)
{
    chkCode += character[rnd.Next(character.Length)];
}
//把验证码存入session
Session["chkCode"] = chkCode;
Bitmap bmp = new Bitmap(65, 20);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);//背景色
//画噪线 
for (int i = 0; i < 0; i++)
{
    int x1 = rnd.Next(65);
    int y1 = rnd.Next(20);
    int x2 = rnd.Next(65);
    int y2 = rnd.Next(20);
    Color clr = color[rnd.Next(color.Length)];
    g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
    string fnt = font[rnd.Next(font.Length)];
    Font ft = new Font(fnt, 15);
    Color clr = color[rnd.Next(color.Length)];
    g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr),
        (float)i * 15 + 2, (float)0);
}
//画噪点 
for (int i = 0; i < 50; i++)
{
    int x = rnd.Next(bmp.Width);
    int y = rnd.Next(bmp.Height);
    Color clr = color[rnd.Next(color.Length)];
    bmp.SetPixel(x, y, clr);
}
//清除该页输出缓存,设置该页无缓存 
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
//将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
MemoryStream ms = new MemoryStream();
try
{
    bmp.Save(ms, ImageFormat.Png);
    Response.ClearContent();
    Response.ContentType = "image/Png";
    Response.BinaryWrite(ms.ToArray());
}
finally
{
    //显式释放资源 
    bmp.Dispose();
    g.Dispose();
}
xoyozo 17 年前
5,525

推荐参考更新的文章:.NET 5 / ASP.NET Core / ASP.NET Frameworks 从纯真 IP 数据库(QQWry.Dat)中查询 IP 归属地

纯真IP数据库官方下载地址:http://www.cz88.net/

之前我写过将 QQWry.Dat 转为 Access 格式后,在 ASP.NET 中通过简单算法去查询,缺点是 IP 数据库体积变大。现在有了更简单的方法:

1,下载 IPLocation.dll备用地址),将它放在网站的 Bin 目录下,将 QQWry.Dat 放在网站的任何位置(一般为 App_Data 目录)。刷新解决方案管理器。

2,在使用的页面加入引用

using IPLocation;

 

或者在 web.config 文件中,节点 pages -> controls 添加

<add tagPrefix="IPLocation" namespace="IPLocation" assembly="IPLocation"/>

 

3,调用方法:

Label1.Text = IPLocation.IPLocation.IPLocate(Server.MapPath("~/App_Data/QQWry.Dat"), TextBox1.Text.Trim());

 

方法 IPLocate() 参数一为 QQWry.Dat 路径,参数二为 IP 地址,返回 IP 归属地。

够简单吧,补充一点,在 VS2003 需要在项目中添加 IPLocation.dll 的引用。

xoyozo 17 年前
6,347

研究了两个小时,确认了一个BUG。

平台:VS2005,VS2008 (VS2003未知)

Framework:2.0,3.5 (1.1未知)

首先跟我做一遍,非常简单:

一、在 VS 中新建网站;
二、已有 Default.aspx,再建 Default2.aspx;
三、在这两个网页的 Page_Load 事件上分别加上断点
四、在 Default2.aspx 中拖入一个 ImageButton,并设该页为起始页
五、运行。

这时可以发现,程序在两处断点的地方都会停下来,而这两上网页根本就没有任何关系,只是在同一级目录而已。

经测试,ImageMap 控件也有同样的现象,其它的就没一一去试了。

建议大家尽量用Button代替ImageButton。按照下面的做法可以把Button美化成和ImageButton一样的效果,甚至更棒!

方法如下:

给 Button 加上 CssClass 属性来写样式,或在App_Themes 的 .skin 文件中定义 Button 的属性

<asp:Button runat="server" CssClass="ButtonStyle" />

 

然后在 .css 文件中处理它的样式,例如:

 

.ButtonStyle {
border:0;
color: #FFFFFF;
font-size: 14px;
font-weight:bold;
text-align: center;
vertical-align:middle;
line-height:27px;
height: 27px;
width: 77px;
background-color: transparent;
background-image: url(Images/button.gif);/*背景*/
background-position: center center;
}

 

这样就可以了,这时你会发现写在 Button 上的 Text 会随着你的鼠标按下而偏移,这个效果是不是 ImageButton 所没有的呢?

xoyozo 18 年前
5,903

需要 URLRewriter.dll,它可以在微软官方下载到:取出Bin里面的文件

接下来配置 Web.Config 文件:

在 <configSections> 节点下添加:

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

在 <configuration> 节点下添加:

<RewriterConfig>
<Rules>
<RewriterRule>
<!--测试-->
<LookFor>~/(\d{2})/(\d{2})\.aspx</LookFor>
<SendTo>~/Default.aspx?m=$1&amp;d=$2</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>

在 <httpHandlers> 节点下添加:

<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />


完成以上步骤即可以实现URL重写的。但是它只能实现 ~/11/11.aspx~/Default.aspx?m=11&d=11 的重写,如果要实现 ~/11/11.html~/Default.aspx?m=11&d=11 的重写,那么需要通过设置 IIS 来实现 .html 文件的筛选。(适合 IIS5 IIS6,在 IIS7 中不用这么麻烦,具体见本文末尾的链接,scott 的文章)

进入:Internet 信息管理服务 -》网站属性 -》主目录 -》配置 -》映射 -》添加 -》把扩展名 .html 映射到 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll。

然后在 <httpHandlers> 节点中,把 *.html 也交给 URLRewriter 处理就行了。

OK,试试看,访问 ~/11/11.html 确实是 ~/Default.aspx?m=11&d=11 的内容,可是只要 postback 一下,地址栏又显示实际路径了。只要在你的/app_browsers文件夹里添加一个.browser文件,注册使用一个控件适配类即可输出新的action属性。

 

 

 

你可在这里查看一个我创建的样例实现,其展示了该如何实现与URL重写协作的表单控件适配器(Form Control Adapter)

 

参考:http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
译文:http://blog.joycode.com/scottgu/archive/2007/03/01/94004.aspx

xoyozo 18 年前
5,378
方法 信息量大小 保存时间 应用范围 保存位置
Application 任意大小 整个应用程序的生命期 所有用户 服务器端
Session 小量、简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端
Cookie 小量、简单的数据 可以根据需要设定 单个用户 客户端
Viewstate 小量、简单的数据 一个Web页面的生命期 单个用户 客户端
Cache 任意大小 可以根据需要设定 所有用户 服务器端
隐藏域 小量、简单的数据 一个Web页面的生命期 单个用户 客户端
查询字符串 小量、简单的数据 直到下次页面的跳转请求 单个用户 客户端
Web.Config文件 不变、或极少改奕的小量数据 直到配置文件被更新 所有用户 服务器端
5,371