网站地图

墨客学术服务平台

当前位置: 主页 > 论文发表 >

写自己的ASP.NET MVC框架(下)

时间:2018-12-03 05:32人气:来源: 网络整理

上篇博客【写自己的ASP.NET MVC框架(上)】 我给大家介绍我的MVC框架对于Ajax的支持与实现原理。今天的博客将介绍我的MVC框架对UI部分的支持。

注意:由于这篇博客是基于前篇博客的,因此有些已说过的内容将会直接跳过,也不会给出提示。
所以,如果要想理解这篇博客,那么阅读上篇博客【写自己的ASP.NET MVC框架(上)】则是必要的。

MyMVC的特点

在开发MyMVC的过程中,我吸取了一些ASP.NET WebForm的使用经验,也参考了ASP.NET MVC,也接受了Martin Fowler对于MVC思想的总结。 在设计过程中,我只实现了一些必要的功能,而且没有引入其它的类库与组件,因此,它非常简单,且容易使用。

我们可以这样理解MyMVC:它是一个简单,容易使用,且符合MVC思想的框架。

在MyMVC框架中,View仍然采用了WebForm中的Page,毕竟Page已经使用了十年,能经得起时间的检验,它仍然是我们可信赖的技术。 另一方面,Page也是ASP.NET中默认的HTML输出技术,使用它会比较方便。

MyMVC与微软的ASP.NET MVC不同的是:
1. 不依赖于URL路由组件。
2. 不提供任何HtmlHelper
3. Controller只是一个Action的容器,没有基类的要求。
4. Action处理的请求不区分POST, GET
5. URL可以直接对应一个网站目录中的aspx页面(View)。
6. View的使用是使用路径来指定,与Controller,Action的名字无关。

说明:URL虽然可以与网站中的页面对应,但这种对应并不是必须的,也可以不对应。
而且本质上与WebFrom中的页面执行过程并不相同。
下图反映了在MyMVC中,一个页面请求的执行过程:

介绍示例项目

为了让大家对MyMVC有兴趣,也为了检验MyMVC的设计,我在开发MyMVC的过程,还专门开发一个基于MyMVC的ASP.NET网站示例项目。 网站提供了三种显示风格(也就是三种View),下面以“客户管理”页面为例来展示三种View的不同:

风格1

View对应的代码如下:

<%@ Page Title="客户管理" Language="C#" MasterPageFile="MasterPage.master" Inherits="MyPageView<CustomersPageModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <%= HtmlExtension.RefJsFileHtml("/js/MyPage/Customers.js")%> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <p><a id="btnCreateItem" href="#" class="easyui-linkbutton" iconCls="icon-add">创建客户</a></p> <table class="GridView" cellspacing="0" cellpadding="4" border="0" style="border-collapse:collapse;"> <tr align="left"> <th style="width:20px;">&nbsp;</th> <th style="width:260px;">客户名称</th> <th style="width:80px;">联系人</th> <th>地址</th> <th style="width:80px;">邮编</th> <th style="width:160px;">电话</th> </tr> <% foreach( Customer customer in Model.List ) { %> <tr> <td><a href="/AjaxCustomer/Delete.cspx?id=<%= customer.CustomerID %>&returnUrl=<%= RequestUrlEncodeRawUrl %>" title="删除" class="easyui-linkbutton" plain="true"> <img src="/Images/delete.gif" alt="删除" /></a> </td> <td><a href="#" class="easyui-linkbutton" rowId="<%= customer.CustomerID %>" plain="true" iconCls="icon-open"> <%= customer.CustomerName.HtmlEncode()%></a> </td> <td><span name="ContactName"><%= customer.ContactName.HtmlEncode() %></span> </td> <td><span name="Address"><%= customer.Address.HtmlEncode() %></span> </td> <td><span name="PostalCode"><%= customer.PostalCode.HtmlEncode() %></span> </td> <td><span name="Tel"><%= customer.Tel.HtmlEncode() %></span> </td> </tr> <% } %> <%= Model.PagingInfo.PaginationBar(6)%> </table> <div id="divCustomerInfo" title="客户" style="padding: 8px; display: none"> <%= UcExecutor.Render("/Controls/Style1/CustomerInfo.ascx", Model.Customer)%> </div> </asp:Content>

风格2



本类导航

sitemap | sitemap