how to get all controller attribute action and parameter in asp.net mvc
2012-06-15 17:39
621 查看
正在考虑用mvc对公司网站进行改版,虽然从asp.net mvc最早推出就开始关注,却从来没有做过相关项目,甚至稍微像样的练习都没有做过。权限这部分看了网上很多文章,有的是继承 Authorize来实现的,也有的是用ActionFilter来进行权限控制的,正如网上有文章提出,Authorize本来就是用来做验证的,验证和授权应该分开来,所以我也考虑把权限控制放在了ActionFilter中来处理,这就需要知道当前请求的controller,action,param,当前用户肯定是需要的,需要获得这些,只需要使用 OnActionExecuting(ActionExecutingContext filterContext) 中的 filterContext 就可以获取到:
string controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string action = filterContext.ActionDescriptor.ActionName;
string userName = filterContext.HttpContext.User.Identity.Name;
RouteValueDictionary parameters = filterContext.RouteData.Values;
要获取请求的具体的参数情况再处理一下 parameters 就可以了,这里不详解。
接下来,可以根据数据库中配置的权限信息来判断当前用户是否具有操作此action的权限了。
那如何配置整个项目中的所有的controller action parameter等等这些信息呢,总不至于一个一个的手工去找出来吧,所以在权限配置的页面上可以通过反射获取项目中所有的这些controller action parameter 等等这些信息,参考代码如下:
var allControllers = Assembly.GetCallingAssembly().GetTypes().Where(type => type.IsSubclassOf(typeof(Controller))).ToList();
foreach (var controller in allControllers)
{
foreach (var method in controller.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
if (method.ReturnType == typeof(ActionResult))
{
//获取这些方法的属性,主要需要获取是get还是post,做权限时区分为读写操作
foreach (var attribute in method.GetCustomAttributes(typeof(System.Web.Mvc.HttpPostAttribute), true))
{
HttpContext.Current.Response.Write("[HttpPost]<br>");
}
//获取controller的名称 和 返回类型为ActionResult的方法名称
HttpContext.Current.Response.Write(controller.Name + "." + method.Name + "(");
//获取方法的所有参数,并按照位置顺序排序
var parameters = method.GetParameters().OrderBy(p => p.Position);
foreach (var parameter in parameters)
{
//输出方法中的参数类型及参数名
HttpContext.Current.Response.Write(parameter.ParameterType + " " + parameter.Name);
//如果参数有默认值的话,则输出参数的默认值
if (!string.IsNullOrWhiteSpace(parameter.DefaultValue.ToString()))
{
HttpContext.Current.Response.Write(" = " + parameter.DefaultValue);
}
//判断是否是最后一个参数
if (parameter.Position < parameters.Count() - 1)
{
HttpContext.Current.Response.Write(", ");
}
}
HttpContext.Current.Response.Write(")<br><br>");
}
}
}
上述代码中的结果是输出到网页上的,实际使用可以去掉这些输出,自己定一些变量去保存这些信息,我把我这里输出的格式如下:
AccountController.Login(System.String returnUrl)
[HttpPost]
AccountController.Login(MvcMaxrayweb.Models.LoginModel model, System.String returnUrl)
AccountController.LogOff()
AccountController.Register()
[HttpPost]
AccountController.Register(MvcMaxrayweb.Models.RegisterModel model)
AccountController.ChangePassword()
[HttpPost]
AccountController.ChangePassword(MvcMaxrayweb.Models.ChangePasswordModel model)
AccountController.ChangePasswordSuccess()
HomeController.Index(System.Int32 id = 1, System.Int32 pagesize = 10)
HomeController.About()
HomeController.Contact()
NoPermissionController.Index()
获取attribute是为了在做权限时区分读写操作,具体如何实现权限这部分,等完成这个网站之后再视情况发博。
请尊重我的折腾成果,如需转载请保留以下信息
本文原创地址:/article/5158839.html
作者:陈平
邮箱:chenping@maxray.cn
还是谷歌人性化,谷歌知道今天是我的生日
string controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string action = filterContext.ActionDescriptor.ActionName;
string userName = filterContext.HttpContext.User.Identity.Name;
RouteValueDictionary parameters = filterContext.RouteData.Values;
要获取请求的具体的参数情况再处理一下 parameters 就可以了,这里不详解。
接下来,可以根据数据库中配置的权限信息来判断当前用户是否具有操作此action的权限了。
那如何配置整个项目中的所有的controller action parameter等等这些信息呢,总不至于一个一个的手工去找出来吧,所以在权限配置的页面上可以通过反射获取项目中所有的这些controller action parameter 等等这些信息,参考代码如下:
var allControllers = Assembly.GetCallingAssembly().GetTypes().Where(type => type.IsSubclassOf(typeof(Controller))).ToList();
foreach (var controller in allControllers)
{
foreach (var method in controller.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
if (method.ReturnType == typeof(ActionResult))
{
//获取这些方法的属性,主要需要获取是get还是post,做权限时区分为读写操作
foreach (var attribute in method.GetCustomAttributes(typeof(System.Web.Mvc.HttpPostAttribute), true))
{
HttpContext.Current.Response.Write("[HttpPost]<br>");
}
//获取controller的名称 和 返回类型为ActionResult的方法名称
HttpContext.Current.Response.Write(controller.Name + "." + method.Name + "(");
//获取方法的所有参数,并按照位置顺序排序
var parameters = method.GetParameters().OrderBy(p => p.Position);
foreach (var parameter in parameters)
{
//输出方法中的参数类型及参数名
HttpContext.Current.Response.Write(parameter.ParameterType + " " + parameter.Name);
//如果参数有默认值的话,则输出参数的默认值
if (!string.IsNullOrWhiteSpace(parameter.DefaultValue.ToString()))
{
HttpContext.Current.Response.Write(" = " + parameter.DefaultValue);
}
//判断是否是最后一个参数
if (parameter.Position < parameters.Count() - 1)
{
HttpContext.Current.Response.Write(", ");
}
}
HttpContext.Current.Response.Write(")<br><br>");
}
}
}
上述代码中的结果是输出到网页上的,实际使用可以去掉这些输出,自己定一些变量去保存这些信息,我把我这里输出的格式如下:
AccountController.Login(System.String returnUrl)
[HttpPost]
AccountController.Login(MvcMaxrayweb.Models.LoginModel model, System.String returnUrl)
AccountController.LogOff()
AccountController.Register()
[HttpPost]
AccountController.Register(MvcMaxrayweb.Models.RegisterModel model)
AccountController.ChangePassword()
[HttpPost]
AccountController.ChangePassword(MvcMaxrayweb.Models.ChangePasswordModel model)
AccountController.ChangePasswordSuccess()
HomeController.Index(System.Int32 id = 1, System.Int32 pagesize = 10)
HomeController.About()
HomeController.Contact()
NoPermissionController.Index()
获取attribute是为了在做权限时区分读写操作,具体如何实现权限这部分,等完成这个网站之后再视情况发博。
请尊重我的折腾成果,如需转载请保留以下信息
本文原创地址:/article/5158839.html
作者:陈平
邮箱:chenping@maxray.cn
还是谷歌人性化,谷歌知道今天是我的生日
相关文章推荐
- How to get the Current Controller Name, Action, or ID in ASP.NET MVC
- asp.net mvc get controller name and action name
- How to use Asp.Net Mvc ActionFilterAttribute for form authentication
- How to get URL and QueryString value in an ASP.NET page
- How to get all Errors from ASP.Net MVC modelState?
- ASP.NET MVC: how to include <span> in link from Html.ActionLink?
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core
- How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- Authorize attribute and jquery AJAX in asp.net MVC
- How to create custom html helper in Asp.net MVC 3 or 4
- How to use JSon data in mvc action and post form data use JQuery ajax
- How to get current thread ID in asp.net
- How to Pop Open an image window that resizes to fit the image in ASP.net 1.x and 2.0 Beta 1
- How to use HTML-5 data-* attributes in ASP.NET MVC
- asp.net MVC 之 Controller.RedirectToAction 方法 详解
- How and Why to use the System.servicemodel.MessageParameterAttribute in WCF
- How to get Hidden Column Value in GridView(ASP.net GridView如何获取隐藏列的值)
- How to bind and validate in MultiActionController
- Minify Action Filter Attribute in ASP.NET MVC