上一节中,我们完成了数据访问层的相关任务,包括业务对象,和数据库表关系映射等等,接下来,我们将从业务逻辑去分析ASP.NET MVC,业务逻辑可以说是数据层和视图层的连接层,大量的业务操作都将在这里完成,并在视图层表现出强大的功能.....
一、构建业务逻辑项目
我们将业务逻辑部分写到ASP.NET MVC应用程序的Controllers文件夹下:
具体的MVC中的C即control 的功能这里不再赘述,我们将本系统用到的相关功能通过代码做一下解释....
下面的对Custom即会员板块的业务逻辑进行说明:
2 public ActionResult Index()
3 {
4 return View();
5 }
6
7 CustomDB c = new CustomDB();
8 // 会员部分
9 public ActionResult Person()
10 {
11 return View();
12 }
13
14 // 获取会员信息
15 [AcceptVerbs(HttpVerbs.Post)]
16 public ActionResult GetPerData()
17 {
18 string iden = "" ;
19 string name = "" ;
20 string str = c.QueryCount( int .Parse(Request.Form[ " start " ]), int .Parse(Request.Form[ " limit " ]),iden,name);
21 Response.Write(str);
22 Response.End();
23 Response.Clear();
24 return null ;
25 }
26
27 // 搜索会员信息
28 [AcceptVerbs(HttpVerbs.Post)]
29 public ActionResult GetSData()
30 {
31 string str = c.QueryCount( 0 , 50 , Request.Form[ " c_iden " ], Request.Form[ " c_name " ]);
32 Response.Write(str);
33 Response.End();
34 Response.Clear();
35 return null ;
36 }
37
38 // 删除会员信息
39 [AcceptVerbs(HttpVerbs.Post)]
40 public ActionResult DeletePer()
41 {
42 bool a = c.delete(Request.Form[ " c_iden " ]);
43 if ( ! a)
44 {
45 Response.Write( " success:false " );
46 } else
47 {
48 Response.Write( " success:true " );
49 }
50 Response.End();
51 return null ;
52 }
53
54 // 添加会员
55 public ActionResult AddAccount()
56 {
57 Custom cus = new Custom();
58 cus.c_name = Request.Form[ " c_name " ];
59 cus.c_pwd = Request.Form[ " c_pwd " ];
60 cus.c_iden = Request.Form[ " c_iden " ];
61 cus.c_sex = Request.Form[ " c_sex " ];
62 cus.c_email = Request.Form[ " c_email " ];
63 cus.c_tel = Request.Form[ " c_tel " ];
64 bool a = c.add(cus);
65 if ( ! a)
66 {
67 Response.Write( " {success:false} " );
68 }
69 else
70 {
71 Response.Write( " {success:true} " );
72 }
73 Response.End();
74 return null ;
75 }
76
77 // 更新会员信息
78 [AcceptVerbs(HttpVerbs.Post)]
79 public ActionResult UpdatePer()
80 {
81 int idd = int .Parse(Request.Form[ " c_id " ]);
82 Custom cus = new Custom();
83 cus.c_name = Request.Form[ " c_name " ];
84 cus.c_iden = Request.Form[ " c_iden " ];
85 cus.c_pwd = Request.Form[ " c_pwd " ];
86 cus.c_sex = Request.Form[ " c_sex " ];
87 cus.c_email = Request.Form[ " c_email " ];
88 cus.c_tel = Request.Form[ " c_tel " ];
89 cus.c_integral = int .Parse(Request.Form[ " c_integral " ]);
90 cus.c_level = int .Parse(Request.Form[ " c_level " ]);
91 bool a = c.update(idd, cus);
92 if ( ! a)
93 {
94 Response.Write( " {success:false} " );
95 }
96 else
97 {
98 Response.Write( " {success:true} " );
99 }
100 Response.End();
101 return null ;
102 }
那么,第一行index()方法,对应视图中的Index.aspx页面,这里设置了一个过滤器--[AuthenticationFilter],表示只有登录的注册用户才能访问....
ASP.NET MVC中的过滤器功能类似于其在现实中的功能,使用过滤器,我们可以过滤请求或者修改通过的数据。
这个过滤器我们使用ActionFilter来实现,ActionFilter的使用就像使用特性一样,在方法上标记一下就可以使用了.....
2 {
3 public override void OnActionExecuting(ActionExecutingContext filterContext)
4 {
5 if ( ! filterContext.HttpContext.User.Identity.IsAuthenticated)
6 {
7 filterContext.Result = new RedirectToRouteResult(
8 " Default " , new RouteValueDictionary( new { action = " Login " }));
9 }
10 }
11 }
在ASP.NET MVC中,将读取表单数据,在控制器中大部分我们都使用选择器AcceptVerbsAttribute,我们可以限制一个动作是“POST”.....
那么具体的方法内容实现的功能我们在View即页面讲述的时候贴出来对应前台效果讲解........
一、个性化设置
1、其中ASP.NET中,原始的错误总是暴露出大量的网站源码,给网站带来隐患的同时用户界面不友好体现的淋漓尽致....
那么这里,我们根据不同异常种类,设置了几种异常处理页面,页面内容仿照博客园的异常页面:
首先需要在全局应用程序类Global.asax.cs中,设置个性化异常处理的具体实现代码:
2 {
3 Exception exception = Server.GetLastError();
4 Response.Clear();
5
6 HttpException httpException = exception as HttpException;
7 RouteData routeData = new RouteData();
8 routeData.Values.Add( " controller " , " Error " );
9
10 if (httpException == null )
11 {
12 routeData.Values.Add( " action " , " Index " );
13 }
14 else
15 {
16 switch (httpException.GetHttpCode())
17 {
18 case 404 :
19 routeData.Values.Add( " action " , " HttpError404 " );
20 break ;
21 case 500 :
22 routeData.Values.Add( " action " , " HttpError500 " );
23 break ;
24 default :
25 routeData.Values.Add( " action " , " General " );
26 break ;
27 }
28 }
29
30 routeData.Values.Add( " error " , exception);
31 Server.ClearError();
32 IController errorController = new ErrorController();
33 errorController.Execute( new RequestContext(
34 new HttpContextWrapper(Context), routeData));
35 }
其中包括:错误404(找不到页面的异常)、错误500(服务器端的异常)、应用程序逻辑方面的异常以及其他异常....
相应的我们建立一个ErrorController控制器....
2 [OutputCache(Location = OutputCacheLocation.None)]
3 public class ErrorController : Controller
4 {
5 //
6 // GET: /Error/
7
8 public ActionResult Index( string error)
9 {
10 ViewData[ " Title " ] = " sorry ,application error " ;
11 ViewData[ " Description " ] = error;
12 return View();
13 }
14
15 public ActionResult HttpError404( string error)
16 {
17 ViewData[ " Title " ] = " 404错误:资源不存在 " ;
18 ViewData[ " Description " ] = error;
19 return View();
20 }
21
22 public ActionResult HttpError500( string error)
23 {
24 ViewData[ " Title " ] = " sorry ,500 error " ;
25 ViewData[ " Description " ] = error;
26 return View();
27 }
28
29 public ActionResult General( string error)
30 {
31 ViewData[ " Title " ] = " sorry , error " ;
32 ViewData[ " Description " ] = error;
33 return View( " Index " );
34 }
35 }
同样的,我们设置了捕获异常的过滤器---HandlerError以及相关缓存的设置....
效果图:
2、网站地图
网站地图可以使你在一个XML文件(web.sitemap)定义网站的层次结构,在页面上可以设置SitemapPath控件,那么我们主要通过这个控件显示面包屑导航,使用户方面返回更高级页面...
2 < siteMap xmlns = " http://schemas.microsoft.com/AspNet/SiteMap-File-1.0 " >
3 < siteMapNode url = " /Home " title = " Home " description = "" >
4 < siteMapNode url = " /Home/Index " title = " Home " description = "" />
5 < siteMapNode url = " /Home/Search " title = " Search " description = " Search " />
6 < siteMapNode url = " /Home/Map " title = " Map " description = " Map " />
7 < siteMapNode url = " /Account/Index " title = " PCenter " description = " PCenter " >
8 < siteMapNode url = " /Account/Login " title = " Login " description = " Login " />
9 < siteMapNode url = " /Account/Register " title = " Register " description = " Register " />
10 </ siteMapNode >
11 < siteMapNode url = " /Home/Picture " title = " View " description = " View " />
12 < siteMapNode url = " /Home/About " title = " About " description = " About " />
13 </ siteMapNode >
14
15 </ siteMap >
在aspx页面,我们可以用过服务器控件SiteMapPath显示给用户...
< asp:SiteMapPath ID = " smp " runat = " server " />
</ div >
然而,我发现这种方式的面包屑导航有很不友好的一面,就是当我打开网站的默认首页的时候,以为没有对页面上的导航做任何行为,所以面包屑为空....定制SitemapProvider可以达到更好的效果....本系统没有设置SitemapProvider,笔者也在学习中,呵呵....
总结:
业务逻辑层的编写在MVC环节中占据了很重要的一部分,它关系到数据层与视图层数据上的承接....
但是就返回类型上,有时候遇到逻辑上的短路还是不懂得如何处理,就比如Ext中的charts组件,当需要返回每个月销售情况的时候,出于对LINQ语句的研究不够深入,分组的时候遇到了问题......有待研究....
接下来,将对每个页面的页面和功能实现讲解一下,前台使用了jquery技术,所以对js 的引用将会比较多....
各位,晚安!!!!