webform版部分视图与请求拦截

1.主控前台页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>

<%@ Import Namespace="WebApplication4" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="Scripts/jquery-1.8.1.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <input type="button" id="rskbook" name="rskbook" value="风险计量" partialview="~/Views/HtmlPage1.html" class="qie" />
        <input type="button" id="rskfactor" name="rskfactor" value="风险因子" partialview="~/Views/HtmlPage2.html" class="qie" />
        <div id="contents">

        </div>
    </form>
    <script type="text/javascript">
        $(function () {
            $(".qie").on("click", function () {
                var url = $(this).attr("partialview");
                alert(url+"!!!")
                $.post("webform1.aspx?_method=gethtml&url=" + url, function (data) {
                    data = JSON.parse(data);
                    $("#contents").html(data.html);
                });
               
            });
            var url = "webform1.aspx?controller=Person&action=GetAge";
            $.post(url, function (data) {
                data = JSON.parse(data);
                alert("成功");
            });
        });
    </script>
</body>
</html>

2.主控后台
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;

namespace WebApplication4
{
    public class aaAttribute : Attribute
    {

    }
    public class ControllerBase
    {

    }
    public class Person : ControllerBase
    {
        [aaAttribute]
        public string GetAge()
        {
            return "我3岁了";
        }
    }
    public static class Html
    {
        public static XmlNode getNode(string xmlPath,string url)
        {
            return getNode(xmlPath, AppDomain.CurrentDomain.BaseDirectory + url.Replace("~",""),0);
        }
        public static XmlNode getNode(string xmlPath, string fileName,int i)
        {

            XmlNode node = null;
            string root = xmlPath;
            if (node == null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);
                node = xmlDoc.SelectSingleNode(xmlPath);
               
            }
            return node;
        }
        public  static string GetFilePath(string filePath)
        {
            return AppDomain.CurrentDomain.BaseDirectory + filePath.Replace("~/", "").Replace("/", @"");
          
        }
        public static string PartialView(string url)
        {
  
            string script = getNode("//html//head//script",url).OuterXml;
            string body = getNode("//html//body",url).InnerXml;
            string content = body.ToString() + "
"  +script.ToString();
            return content;
        }
    }
    public partial class BasePage : System.Web.UI.Page
    {
        public object GetObject(Type t)
        {
            return new Person();
        }
        protected virtual void Page_Load(object sender, EventArgs e)
        {

            string controller = Request["controller"];
            string action = Request["action"];
            if (!string.IsNullOrEmpty(controller))
            {
                var types = typeof(BasePage).Assembly.GetTypes();
                Dictionary<Type, List<MethodInfo>> controllerActionHt = new Dictionary<Type, List<MethodInfo>>();
                MethodInfo[] methods = null;
                foreach (Type type in types)
                {
                    //初始化controller
                    if (!controllerActionHt.ContainsKey(type))
                    {
                        controllerActionHt.Add(type, new List<MethodInfo>());
                    }
                    //添加action
                    methods = type.GetMethods();
                    foreach (MethodInfo m in methods)
                    {
                        (controllerActionHt[type]).Add(m);
                    }
                }
                var ct = (from c in controllerActionHt
                          where c.Key.Name == controller
                           && c.Value.Any(x => x.Name == action)
                          select new { controller = c.Key, action = c.Value.FirstOrDefault() }).FirstOrDefault();
                MethodInfo method = ct.action;
                IEnumerable<Attribute> attrs = method.GetCustomAttributes();
                object result = null;
                foreach (var attr in attrs)
                {
                    string name = attr.GetType().Name;
                    result=method?.Invoke(GetObject(ct.controller), null);

                }
                Response.Write(1);
                Response.End();
             
            }

        }
    }
    public partial class WebForm1 : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            switch (Request["_method"])
            {
                case "gethtml":
                    string url = Request["url"];
                    var html = Html.PartialView(url);
                    var json = Newtonsoft.Json.JsonConvert.SerializeObject(new { html = html });
                    Response.Write(json);
                    Response.End();
                    break;
                default:
                    break;
            }
            base.Page_Load(sender, e);
        }


    }
}
3.相应的部分视图HtmlPage1
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">
       
        var  Init=function() {
            $("#btn").on("click", function () {
                $("#sub").html('<div>我们都是好孩子11世</div>');
            });
        }()
    </script>
</head>

<body>
    <div id='content'>
        <input type='button' id='btn' name='btn' value='刷新11' />
        <div id="sub">我们都是好孩子1世</div>
    </div>
</body>

</html>

4.部分视图HtmlPage2
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript">

        var Init = function () {
            $("#btn").unbind();
            $("#btn").on("click", function () {
                $("#sub").html('<div>我是超人马苏丹22世</div>');
            });
        }()
    </script>
</head>

<body>
    <div id='content'>
        <input type='button' id='btn' name='btn' value='刷新22' />
        <div id="sub">我们都是好孩子2世</div>
    </div>
</body>

</html>
原文地址:https://www.cnblogs.com/kexb/p/6637733.html