我的应用程序中有一个ActionFilterAttribute,用于在检测到用户未通过身份验证时重定向用户。在过滤器中,我想检测动作的ReturnType何时是JsonResult。如何在ActionExecutingContext中访问ActionDescriptor的MethodInfo.ReturnType?
作为一种解决方法,我最初创建了IsJsonResult的自定义属性,并在该解决方案中使用该属性修饰了JsonResult方法。这工作,并在行动滤波器实现如下:
public class CheckUser : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
{
base.OnActionExecuting(actionExecutingContext);
object[] customAttributes = actionExecutingContext.ActionDescriptor.GetCustomAttributes(true);
bool isJsonResult = customAttributes.FirstOrDefault(a => a.GetType() == typeof(IsJsonResult)) != null;
if (isJsonResult)
{
return; // Don't perform any additional checks on JsonResult requests.
}
// Additional checking code omitted.
}
}
这一工程,但我不喜欢装饰所有的JsonResult动作在这个项目的想法。如果新的JsonResult被添加到项目中,那很容易失败,并且我们忘记相应地修饰它。此外,我可以看到名为“JsonResult”的ReturnType在上面显示的actionExecutingContext对象内的调试器中。这是在监视窗口中显示的路径:
actionExecutingContext > ActionDescriptor > [System.Web.Mvc.ReflectedActionDescriptor] > MethodInfo > ReturnType > FullName
这FullName属性有“System.Web.Mvc.JsonResult”的值。
因此,我似乎可以直接从actionExecutingContext对象中提取该值,并创建一个支持方法来返回一个bool指示符。为了达到这个目的,下面是我写的代码。
private bool isReturnTypeJson(ActionExecutingContext actionExecutingContext)
{
string actionName = actionExecutingContext.ActionDescriptor.ActionName;
string controllerName = actionExecutingContext.ActionDescriptor.ControllerDescriptor.ControllerName;
Type controllerType = actionExecutingContext.Controller.GetType();
try
{
// Only effective when the actionName is not duplicated in the controller.
Type returnType = controllerType.GetMethod(actionName).ReturnType;
return (returnType.Name == "JsonResult");
}
catch (AmbiguousMatchException)
{
// Using LINQ, can I filter this collection to isolate just the methods
// that have the same name as the "actionName" variable above?
MethodInfo[] methodsInfoCollection = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
// Attempted code from https://stackoverflow.com/questions/15283158/net-mvc-counting-action-methods-in-web-application
//var info = typeof(controllerType)
// .Assembly.GetTypes()
// .Where(t => typeof(Controller).IsAssignableFrom(t))
// .Where(t => t.Namespace.StartsWith("AwesomeProduct.Web"))
// .SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
// .Where(m => typeof(ActionResult).IsAssignableFrom(m.ReturnType))
// .Where(m => !m.IsAbstract)
// .Where(m => m.GetCustomAttribute<NonActionAttribute>() == null);
}
return false;
}
只要操作名称在控制器中唯一,就可以在try-block中分配returnType。但是,如果控制器中存在多个相同操作名称的实例,则会发生AmbiguousMatchException。所以在catch-block中,我已经将方法分配给一个集合。 使用LINQ,我怎样才能将来自methodsInfoCollection变量的值过滤到通过ActionExecutingContext到达的动作?
我研究的一些文章在下面,并且使用了一些这方面的想法。但我还没有弄明白。
- http://www.codeproject.com/Articles/742461/Csharp-Using-Reflection-and-Custom-Attributes-to-M
- Can I get an Action's return type from an Action Filter?
- .NET MVC: Counting Action methods in web application
感谢您的帮助。
==============
更新至原始代码。这有效,但循环并不理想。
private bool isReturnTypeJson(ActionExecutingContext actionExecutingContext)
{
string actionName = actionExecutingContext.ActionDescriptor.ActionName;
string controllerName = actionExecutingContext.ActionDescriptor.ControllerDescriptor.ControllerName;
Type controllerType = actionExecutingContext.Controller.GetType();
try
{
// Only effective when the actionName is not duplicated in the controller.
Type returnType = controllerType.GetMethod(actionName).ReturnType;
return (returnType.Name == "JsonResult");
}
catch (AmbiguousMatchException)
{
// Using LINQ, can I filter this collection to isolate just the methods with a name of actionName.
MethodInfo[] methodInfoCollection = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
foreach (MethodInfo methodInfo in methodInfoCollection)
{
if (methodInfo.ReturnType != null) {
if (methodInfo.ReturnType == typeof(ActionResult))
{
return false;
}
if (methodInfo.ReturnType == typeof(JsonResult))
{
return true;
}
}
}
}
return false;
}
请参阅下面的Reza Aghaei解决方案。他的解决方案完全消除了对单独方法的需求。
谢谢@Reza Aghaei。 filterContext.Result.GetType()== typeof(JsonResult);返回一个NullReferenceException。 Result对象为null。你会认为这将是人口稠密的,这是最直观的方法。 –
是的,我检查后,我删除了我的评论;) –
我真的很感谢你的帮助。我更新了方法来循环收集。这是可行的,但并不理想。请参阅原始帖子中的调整。 –