惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
Y
Y Combinator Blog
雷峰网
雷峰网
Last Week in AI
Last Week in AI
Jina AI
Jina AI
月光博客
月光博客
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
H
Heimdal Security Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
AWS News Blog
AWS News Blog
NISL@THU
NISL@THU
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
P
Privacy International News Feed
I
Intezer
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
L
LINUX DO - 最新话题
S
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
Security Latest
Security Latest
PCI Perspectives
PCI Perspectives
The Hacker News
The Hacker News
T
Threatpost
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Latest news
Latest news
L
LINUX DO - 热门话题
J
Java Code Geeks
A
Arctic Wolf
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog

博客园 - Awen

asp.net插件实现 一些顶级的设计站点(陆续更新) asp.net定时任务实现(原创) 基于Monorail的系统功能模块化 SubSonic:我想要的动态查询 SubSonic:亚音速之光 一个顶N个的NextResult 自娱自乐 3.5 -开篇有益 WinFormUI-Docking初级使用 一个喜欢网页设计的程序员 一个关于Excel的处理类(部分参考网络上) 抛开映射关系,不oo的数据访问写法 Javascript改观过程2 Javascript改观过程1 - Awen - 博客园 CEIMS项目笔记2:重复的,让工具做去! 备份&收集(持续更新)! CEIMS开发日记:项目计划 Jsp,.net! asp.net优化探讨系列(3)
Monorail的一些常用的东西(验证码,分页。。。持续更新)
Awen · 2008-06-16 · via 博客园 - Awen

1.验证码的使用,我们知道,基于Castle的mvc框架开发,原来基于asp.net WebForm 模型的一些对象就不能正常使用了,不过好在框架本身也提供了天然的对应的解决方案。例如Response对象等。以往我们在asp.net WebForm模型下基于流的输出,这个对象几乎是不可或缺的,现在转到Castle MVC框架下,也有相对应的Response对象,使用上几乎和WebForm一样,所以十分方便!下面是代码!

using System;
using System.Collections.Generic;
using System.Text;
using Castle.MonoRail.Framework;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
namespace AtomPortal.Controllers
{
    
public class ValidateController:Controller
    
{
        
public void index()
        
{
            CancelLayout();
            CancelView();
            
string VNum = MakeValidateCode(4);
          Session[
"code"= VNum;
            CreateImage(VNum);
        }

        
private string MakeValidateCode(int count)
        
{
           
            
            
//此方法用于生成随机码
            char[] s = new char[] '0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f''h''h''i''j''k''l''m''n''o''p''q''r''s''t''u''v''w''x''y''z' };//定义一个组成含有验证码元素的数组
            string num = "";
            Random r 
= new Random();
            
for (int i = 0; i < count; i++)//控制生成随机码的个数
            {
                num 
+= s[r.Next(0, s.Length)].ToString();//在s中随机 抽取一个数添加到num里

            }

            
return num;
        }

        
private void CreateImage(string checkCode)
        
{

            
int iwidth = (int)(checkCode.Length * 15);
            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap(iwidth, 25);
            Graphics g 
= Graphics.FromImage(image);
            g.Clear(Color.White);
            
//定义颜色
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            
//定义字体           
            string[] font = "Verdana""Microsoft Sans Serif""Comic Sans MS""Arial""宋体" };
            Random rand 
= new Random();
            
//随机输出噪点
            for (int i = 0; i < 50; i++)
            
{
                
int x = rand.Next(image.Width);
                
int y = rand.Next(image.Height);
                g.DrawRectangle(
new Pen(Color.LightGray, 0), x, y, 11);
            }


            
//输出不同字体和颜色的验证码字符
            for (int i = 0; i < checkCode.Length; i++)
            
{
                
int cindex = rand.Next(7);
                
int findex = rand.Next(5);

                Font f 
= new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
                Brush b 
= new System.Drawing.SolidBrush(c[cindex]);
                
int ii = 4;
                
if ((i + 1% 2 == 0)
                
{
                    ii 
= 2;
                }

                g.DrawString(checkCode.Substring(i, 
1), f, b, 3 + (i * 12), ii);
            }

            
//画一个边框
            g.DrawRectangle(new Pen(Color.Black, 0), 00, image.Width - 1, image.Height - 1);

            
//输出到浏览器
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            
{
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.ClearContent();
                Response.ContentType 
= "image/Jpeg";
                Response.BinaryWrite(ms.ToArray());
                g.Dispose();
                image.Dispose();
            }


        }

    }

}

接下来使用就和原来的一样,用img标签,地址指向这个Action,另外文件的下载等也可以用相同的方式。
2.分页是一个应用程序必不可少的部分,而且重复度很高,最好是有基于组件的方案,可以大大的提高开发的速度,webForm下我们有很多选择,AspNetPage等等,而在Castle MVC框架下,我们也有许多可用的方案,有以ViewComponents方式的,也有以Helper的方式的,PageHelper由于涉及数据,这里就不说了,而以ViewComponents方式的有Castle.MonoRail.Framework.ViewComponents.DiggStylePagination,可以产生像Digg那样的分页,这个只负责呈现分页导航,与具体的数据呈现无关,和AspNetPage有异曲同工之妙!
使用例子

 public void page(int page)
        
{
Castle.MonoRail.Framework.Helpers.Page a 
= new Castle.MonoRail.Framework.Helpers.Page(null, page, 101000);         
            PropertyBag[
"num"= page;
            PropertyBag[
"str"= a;
        }

view里面就#component(DiggStylePagination with "Page=$str"),具体可复制代码看测试效果!