代码改变世界

分享 ASP.NET WebForm 另外一种开发方式,逃离服务器控件

2012-06-14 18:29  音乐让我说  阅读(539)  评论(1编辑  收藏  举报

代码源于博客:http://www.cnblogs.com/fish-li/archive/2011/12/27/2304063.html

其中我修改和增加了一点点代码。废话不多说,直接贴代码了:

解决方案目录:

PageAdapter 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;


// 为了不写一堆机械式的判断代码,这里就写个简单的基类,统一处理页面的提交动作。
// 注意:代码仅做演示用,并没有过多考虑性能及安全细节。

// 代码源于博客:http://www.cnblogs.com/fish-li/archive/2011/12/27/2304063.html
// 现整理于此。

namespace DearBruce.CustomPageAdapter.CoreLib
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class SubmitMethodAttribute : Attribute
    {
        public bool AutoRedirect { get; set; }
    }

    internal sealed class MethodInvokeInfo
    {
        public MethodInfo MethodInfo;
        public SubmitMethodAttribute MethodAttribute;
    }

    public class MyPageAdapter : System.Web.UI.Adapters.PageAdapter
    {
        public MyPageAdapter()
        {

        }

        private static readonly Hashtable s_table = Hashtable.Synchronized(new Hashtable());

        /// <summary>
        /// 得到要调用的方法
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static MethodInvokeInfo[] GetMethodInfo(Type type)
        {
            MethodInvokeInfo[] array = s_table[type.AssemblyQualifiedName] as MethodInvokeInfo[];
            if (array == null)
            {
                array = (from m in type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                         let a = m.GetCustomAttributes(typeof(SubmitMethodAttribute), false) as SubmitMethodAttribute[]
                         where a.Length > 0
                         select new MethodInvokeInfo { MethodInfo = m, MethodAttribute = a[0] }).ToArray();

                s_table[type.ToString()] = array;
            }
            return array;
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Page.Request.Form.AllKeys.Length == 0)
                return;    // 没有提交表单

            Type currentType = Page.GetType();
            Type baseType = currentType.BaseType;
            MethodInvokeInfo[] array = GetMethodInfo(baseType);
            if (array.Length == 0)
                return;

            foreach (MethodInvokeInfo m in array)
            {
                string actionName = Page.Request.Form[m.MethodInfo.Name]; // 得到要调用的方法,根据客户端提交按钮(input type="submit" name="btnLogin")得到

                if (!string.IsNullOrEmpty(actionName))
                {
                    m.MethodInfo.Invoke(Page, null); // 这里就开始调用这个方法了

                    if (m.MethodAttribute.AutoRedirect && !Page.Response.IsRequestBeingRedirected)
                    {
                        // 说明该 Action 方法里面没有重定向操作,且该 Action 方法标记了 AutoRedirect = true
                        Page.Response.Redirect(Page.Request.RawUrl);
                    }
                    return;
                }
            }
        }
    }
}

 

Page.browser 文件:

<browsers>
  <browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.Page"
                     adapterType="DearBruce.CustomPageAdapter.CoreLib.MyPageAdapter, DearBruce.CustomPageAdapter.CoreLib" />
    </controlAdapters>
  </browser>
</browsers>

index.aspx

<%@ Page Language="C#" CodeBehind="index.aspx.cs" Inherits="DearBruce.CustomPageAdapter.WebUI.index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <div>
        <fieldset>
            <legend>普通登录</legend>
            <form action="<%= Request.RawUrl %>" method="post">
                登录名:<input type="text" name="loginName" style="width: 200px" value="bruce" />
                <input type="submit" name="NormalLogin" value="登录" />
            </form>
            <br /><br /><br />
            <% if (!string.IsNullOrEmpty(LoginName))
               {
                   %>
            <div>
                您刚刚输入的登录名:<%= LoginName %>
            </div>
                   <%
               } %>

        </fieldset>
    </div>
</body>
</html>

 

index.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DearBruce.CustomPageAdapter.CoreLib;

namespace DearBruce.CustomPageAdapter.WebUI
{
    public partial class index : System.Web.UI.Page
    {
        protected string LoginName = null;

        public index()
        {

        }

        [SubmitMethod(AutoRedirect = false)] // 该方法处理完毕后,不自动重定向(Redirect)到本页面,而自动响应
        public void NormalLogin()
        {
            // -----------------------------------------------------------------
            // 注意:演示代码为了简单,这里不检查用户名与密码是否正确。
            // -----------------------------------------------------------------

            string loginName = Request.Form["loginName"];
            if (string.IsNullOrEmpty(loginName))
            {
                return;
            }
            LoginName = loginName;
        }
    }
}

 

运行截图:

 

 

谢谢浏览!