ASP.NET MVC 扩展方法

一、扩展方法的语法

在视图中使用扩展方法的时候 如果扩展方法定义的类在其他命名空间,需要首先引用该命名空间,才能使用该扩展方法

static class 静态类名

{

static 返回类型 扩展方法名(this 扩展的类型 对象名,[其他参数列表])

{

//扩展方法代码

}

}

eg1:给String类型增加一个判断字符串是否为整数的扩展方法

 1        /// <summary>
 2             /// 给String类型增加一个判断字符串是否为整数的扩展方法
 3             /// </summary>
 4             public static class StringExt
 5             {
 6                 public static bool IsInt(this String str)
 7                 {
 8                     int i;
 9                     return int.TryParse(str, out i);
10                 }
11             }
12 
13             public class Program
14             {
15                 static void Main(string[] args)
16                 {
17                     String str = "123";
18                     if (str.IsInt())
19                     {
20                         Console.WriteLine(str + "是整数");
21                     }
22                     else
23                     {
24                         Console.WriteLine(str + "不是整数");
25                     }
26                     Console.ReadLine();
27                 }
28             }

eg2:定义学生类的扩展方法,用来实现验证邮箱

学生类:

 1                 public class Student
 2                 {
 3                     public int id { get; set; }
 4                     public string name { get; set; }
 5                     public string  email { get; set; }
 6 
 7                     public Student() { }
 8                     public Student(int _id,string _name,string _email)
 9                     {
10                         id = _id;
11                         name = _name;
12                         email = _email;
13                     }
14                 }    

扩展方法类:

 1             //邮箱验证扩展方法
 2           public static bool IsValidEmail(this Student stu)
 3           {
 4               int i = stu.email.LastIndexOf('@');
 5               int j = stu.email.LastIndexOf('.');
 6               if(i<j)
 7               {
 8                   return true;
 9               }
10               else
11               {
12                   return false;
13               }
14           }
15                 public class Program
16                 {
17                     static void Main(string[] args)
18                     {
19                         Student stu = new Student(1, "Mary", "abc@qq.com");
20 
21 
22                         bool rt = stu.IsValidEmail();        //调用扩展方法
23                         Console.WriteLine(rt);
24 
25                         rt = StuExtend.IsValidEmail(stu);      //调用扩展方法
26                         Console.WriteLine(rt);
27                         Console.ReadLine();
28                     }
29                 }    

eg3:扩展HtmlHelper

扩展方法类

 1              public static class HtmlExtension
 2                 {
 3                     /// <summary>
 4                     /// 输出表单提交按钮
 5                     /// </summary>
 6                     /// <param name="htmlHelper">HtmlHelper对象</param>
 7                     /// <param name="name"></param>
 8                     /// <param name="value"></param>
 9                     /// <returns></returns>
10                     public static MvcHtmlString Submit(this HtmlHelper htmlHelper,string name,string value)
11                     {
12                         //TagBuilder 用于辅助创建标记
13                         var builder = new TagBuilder("input");
14                         builder.MergeAttribute("type","submit");
15                         //设置value属性
16                         builder.MergeAttribute("value",value);  
17                         //设置name属性
18                         builder.MergeAttribute("name",name);
19                         //设置id属性
20                         builder.GenerateId(name);
21                         return MvcHtmlString.Create(builder.ToString());
22                     }
23 
24                     public static MvcHtmlString Submit(this HtmlHelper htmlHelper, string name, string value, object htmlAttributes)
25                     {
26                         //TagBuilder 用于辅助创建标记
27                         var builder = new TagBuilder("input");
28                         builder.MergeAttribute("type", "submit");
29                         //设置value属性
30                         builder.MergeAttribute("value", value);
31                         //设置name属性
32                         builder.MergeAttribute("name", name);
33                         //设置id属性
34                         builder.GenerateId(name);
35                         //设置其他属性
36                         builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
37                         return MvcHtmlString.Create(builder.ToString());
38                     }
39                 }    

//页面中进行使用

 1             @{
 2                     Layout = null;
 3                 }
 4                 @using ExtensionMethods.App_Code;
 5 
 6                 <!DOCTYPE html>
 7 
 8                 <html>
 9                 <head>
10                     <meta name="viewport" content="width=device-width" />
11                     <title>Index</title>
12                 </head>
13                 <body>
14                     <div>
15                         @Html.Submit("login","登录")
16 
17                         @Html.Submit("login", "登录", new { @class="opt_sub",title="点击提交"})
18                     </div>
19                 </body>
20                 </html>