微软今年发布了 MVC3,以前接触过JAVA中的SSH,感觉思想不错,非常喜欢Struts2中的拦截器的概念,因为做程序也没多久所以一直没机会在WebForm中研究下如何实现IOC及AOP,看到了MVC3觉得大爱,感觉用来解决系统中权限控制到按钮非常不错。以前做的几个项目权限都是设置到页面,没有深入到按钮,一是项目中用不到,二是自己比较懒没有去研究。
用MVC3实现起来非常的简单,因为MVC中所有的请求都是向Controller来取得,不是WebFrom直接访问Aspx页面,可以在每个方法前面打个标签即可,在WebFrom中应该使用Attribute也可以吧?没研究过。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public
class
RoleFilter : FilterAttribute, IAuthorizationFilter
{
#region IAuthorizationFilter 成员
/// <summary>
/// 产生随机数判断是否具有权限访问
/// </summary>
/// <param name="filterContext"></param>
public
void
OnAuthorization(AuthorizationContext filterContext)
{
Random random =
new
Random();
int
i = random.Next(0, 10);
if
(i > 3)
{
filterContext.Result =
new
RedirectResult(ConfigurationManager.AppSettings[
"Url"
] +
"/Error/Index/"
+ i);
}
}
#endregion
}
|
代码比较简单产生个随机数,如果大于3跳转到错误页面~~这个地方可以从数据库中读取权限配置~~
使用起来就更简单了~
1
2
3
4
5
6
7
|
[RoleFilter()]
public
ActionResult Del(
int
id)
{
AlbumModels albumModels = GetModels();
albumModels.DelAlbum(id);
return
View();
}
|
这样即可完成权限~是不是非常简单,同时感觉MVC3中后台直接支持Json数据也非常不错,最近刚研究JQuery所以用到Json的地方还是比较多