Asp.Net MVC实践 (基于ASP.NET MVC Preview 2)
作者:Leven 日期:2008-05-27 22:33:23
以前一直对.Net的表现业务感觉不爽,上次和Forever讨论很久,也没能很好的解决表现成业务分离的问题,最近看到了新的Asp.Net MVC框架,恍然茅塞顿开,原来如此,以前的WebForm基于MVP方式,这次新的MVC框架将表现成很轻松的分离这些业务.
Ok,今天我就来试着用MVC的方式写一个小的Demo程序,这个程序非常简单,就是一个Article的发布和浏览程序,程序有两个表,分别是category和content,分别是分类表,文章表,程序使用MySql数据库,建表脚本如下:
- create table `category`(
- `id` int primary key not null auto_increment,
- `name` varchar(255) not null,
- `intro` varchar(1024),
- `order` int default 1000,
- `url` varchar(255),
- `isurl` bit not null default 0
- );
- ALTER TABLE `category` ADD INDEX ( `order` ) ;
- create table `content`(
- `id` int primary key not null auto_increment,
- `categoryid` int not null,
- `title` varchar(255) not null,
- `info` varchar(1024),
- `content` text,
- `posttime` datetime not null,
- `postuser` varchar(50),
- `hits` int not null default 0
- );
- ALTER TABLE `content` ADD INDEX ( `categoryid` ) ;
- ALTER TABLE `content` ADD INDEX ( `postuser` ) ;
- ALTER TABLE `content` ADD INDEX ( `hits` ) ;
- insert into `category`(`name`,`intro`,`order`,`url`,`isurl`) values
- ('Index','Index Page',0,'/default.aspx',1),
- ('News','业界新闻',1,'',0),
- ('.Net','.Net开发',2,'',0),
- ('Asp.Net MVC','Asp.Net MVC',3,'',0),
- ('C/C++','C/C++开发',4,'',0);
- insert into `content`(`categoryid`,`title`,`info`,`content`,`posttime`,`postuser`,`hits`) values
- (2,'MvcArticle System 1.0发布','MvcArticle System 1.0发布','经过一段时间的开发,MvcArticle System 1.0发布终于发布.',NOW(),'Leven',0);
在程序中,我创建一个HttpModule来初始化整个UrlRouting部分.UrlRouting初始化代码如下:
- private static void InitRoutingUrls(RouteCollection routes)
- {
- routes.Add(new Route(string.Format("article{0}/list/{{category}}/{{page}}", WebConfig.MvcHandle), new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "list", category = "0", page = "1" }),
- Constraints = new RouteValueDictionary(new { category = "d+", page = "d+" }),
- });
- routes.Add(new Route(string.Format("article{0}/index/{{page}}", WebConfig.MvcHandle), new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "index", page = "1" }),
- Constraints = new RouteValueDictionary(new { page = "d+" }),
- });
- routes.Add(new Route(string.Format("article{0}/view/{{id}}", WebConfig.MvcHandle), new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "view", id = "1" }),
- Constraints = new RouteValueDictionary(new { id = "d+" }),
- });
- routes.Add(new Route(string.Format("article{0}/add", WebConfig.MvcHandle), new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "add" }),
- });
- routes.Add(new Route(string.Format("article{0}/edit/{{id}}", WebConfig.MvcHandle), new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "edit", id = "0" }),
- Constraints = new RouteValueDictionary(new { id = "d+" }),
- });
- routes.Add(new Route(string.Format("article{0}/delete/{{id}}", WebConfig.MvcHandle), new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "delete", id = "0" }),
- Constraints = new RouteValueDictionary(new { id = "d+" }),
- });
- routes.Add(new Route(string.Format("article{0}/{{action}}/{{page}}", WebConfig.MvcHandle), new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "index", page = "1" }),
- Constraints = new RouteValueDictionary(new { page = "d+" }),
- });
- routes.Add(new Route("default.aspx", new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "index", page = "1" }),
- });
- routes.Add(new Route("index", new MvcRouteHandler())
- {
- Defaults = new RouteValueDictionary(new { controller = "article", action = "index", page = "1" }),
- });
- }
然后到Web.Config中添加HttpModule
- <add name="UrlRouting" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing"/>
- <add name="UrlRoutingInit" type="MvcArticle.Web.Routing.RoutingModule, MvcArticle.Web"/>
比如在上面的index类中,循环显示ArticleCotent数据的代码如下:
- <%
- foreach (ArticleContent content in ViewData)
- {
- foreach (ArticleCategory cate in MvcArticle.Services.ServicesFactory.GetCategoryServices().Get())
- {
- if (cate.ID == content.CategoryID)
- {
- content.Category = cate;
- }
- }
- %>
- <div class="body_list_item">
- <div class="item_head">
- <%=Html.ActionLink(content.Title, "view",new System.Web.Routing.RouteValueDictionary(new {controller="article", id=content.ID.ToString()}))%><span>作者:<%=content.PostUser %> 日期:<%=content.PostTime %></span>
- </div>
- <div class="item_body">
- <%=content.Info %>
- </div>
- <div class="item_bottom">
- 分类: <%=Html.ActionLink(content.Category.Name, "list", new System.Web.Routing.RouteValueDictionary(new { controller = "article", category = content.CategoryID.ToString(), page = "1" }))%>
- | Hits:<%=content.Hits %>
- | <%=Html.ActionLink("编辑", "edit", new System.Web.Routing.RouteValueDictionary(new { controller = "article", id = content.ID.ToString() })) %>
- | <a href="<%=Url.Action("delete", new System.Web.Routing.RouteValueDictionary(new { controller = "article", id = content.ID.ToString() })) %>" onclick="if (!window.confirm('是否要删除该文章')) return false;" title="删除该文章">删除</a>
- </div>
- </div>
- <%
- }
- %>
最后是关于数据的提交,有两个办法,一个是在Controller的方法中访问Request对象,这个Request对象和WebForm中的保持一致,可以获取Form或者QueryString的内容,当时,更省事的方法是直接对Action方法定义参数,比如Demo的Save方法: public void Save(string title, int categoryid, string postuser, string info, string content)明确定义需要给出title等参数,要post给这种方法,你的aspx中相应的要有这些值,如果没有或者名称不同,则不会获取到你需要的值,比如在要调用该方法的add.aspx中,就分别有name为title,categoryid等的html表单,在提交的时候系统会自动将数据提交给Save方法的参数.
程序执行首页样式示例:
最后给出该Demo的源代码下载(仅MVC部分):
点击下载此文件
上一篇
下一篇
文章来自:
Tags: