• 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>
  • 相关阅读:
    opengl学习
    同步、异步、多线程与事件型综述
    Javascript异步编程的4种方法
    ASP.NET(C#) GridView (编辑、删除、更新、取消)
    浅析五大ASP.NET数据控件
    用 Eclipse 开发 Android 应用程序
    [C# 网络编程系列]专题十:实现简单的邮件收发器
    [C# 网络编程系列]专题九:实现类似QQ的即时通信程序
    [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
    [C# 网络编程系列]专题六:UDP编程
  • 原文地址:https://www.cnblogs.com/kexb/p/6637733.html
Copyright © 2020-2023  润新知