Monorail的一些常用的东西(验证码,分页。。。持续更新)
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, 1, 1);
            }

            //输出不同字体和颜色的验证码字符
            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), 0, 0, 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, 10, 1000);         
            PropertyBag["num"] = page;
            PropertyBag["str"] = a;
        }
view里面就#component(DiggStylePagination with "Page=$str"),具体可复制代码看测试效果!
 
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, 1, 1);
            }
            //输出不同字体和颜色的验证码字符
            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), 0, 0, 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();
            }
        }
    }
}
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, 10, 1000);         
            PropertyBag["num"] = page;
            PropertyBag["str"] = a;
        }
                    
                


    
                
            
        
浙公网安备 33010602011771号