我想要 4 个具有相同名称的操作(控制器方法可能有不同的名称,但它们的名称相同)ActionName()
它们的属性对于所有 4 个都是相同的:
[ActionName("Same-name")]
public ActionResult AnonAction() { ... }
[HttpPost]
[ActionName("Same-name")]
public ActionResult AnonAction(ModelData data) { ... }
[Authorize]
[ActionName("Same-name")]
public ActionResult AuthAction() { ... }
[HttpPost]
[Authorize]
[ActionName("Same-name")]
public ActionResult AuthAction(OtherData data) { ... }
当用户时,第一对夫妇做了一些事情are not经过身份验证(匿名用户)。当用户are已验证。
前三种操作方法按预期工作,但我似乎无法使最后一种方法工作。它抛出一个异常,告诉我它无法区分 POST 操作。我不认为我在这里做错了什么或忘记做某事。我只是希望这不是 Asp.net MVC 2 RC2 中的错误。
有人看出我的行为有什么缺陷吗?
@Paco是对的。AuthorizeAttribute
与动作选择无关。他的建议感觉不对,所以感谢他,我深入研究了 MVC 代码,并自己想出了最合适的解决方案。
解决方案本来就是这样的
MVC 中的这些东西有一个可扩展点。基本上你要做的就是写你自己的ActionMethodSelectionAttribute
这将处理这个问题。我创建了一个根据用户授权(匿名或授权)选择操作的方法。这是代码:
/// <summary>
/// Attribute restricts controller action execution only to either anonymous or authenticated users
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class AllowAuthenticatedAttribute : ActionMethodSelectorAttribute
{
/// <summary>
/// Gets or sets a value indicating whether this <see cref="AllowAuthorizedAttribute"/> allows authenticated or anonymous users to execute decorated controller action.
/// </summary>
/// <value><c>true</c> if authenticated users are allowed to execute the action; <c>false</c> if anonymous users are allowed to execute the action.</value>
public bool Authenticated { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="AllowAuthorizedAttribute"/> class.
/// </summary>
/// <param name="authenticated">If set to <c>true</c> only authorized users will be able to access this action.</param>
public AllowAuthenticatedAttribute(bool authenticated)
{
this.Authenticated = authenticated;
}
/// <summary>
/// Determines whether the action method selection is valid for the specified controller context.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="methodInfo">Information about the action method.</param>
/// <returns>
/// true if the action method selection is valid for the specified controller context; otherwise, false.
/// </returns>
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
return this.Authenticated == controllerContext.HttpContext.User.Identity.IsAuthenticated;
}
}
额外观察
当我用自定义属性装饰我的操作方法时,我仍然遇到相同的异常,直到我添加[HttpGet]
到我的 GET 操作。这是为什么?我在流程图中找到了答案专业 ASP.NET MVC 框架 book (你自己检查一下)。抛出异常是因为有不止一个操作方法ActionMethodSelectorAttribute
。通常我们只装饰 POST 操作,但在这种情况下,所有这些操作都被装饰了。 2 个用于匿名用户,2 个用于经过身份验证的用户。这就是为什么你必须同时使用HttpGet
and HttpPost
关于行动方法当您向它们添加更多选择器属性时。
我的控制器操作现在看起来像这样
[HttpGet]
[AllowAuthenticated(false)]
[ActionName("Same-name")]
public ActionResult AnonAction() { ... }
[HttpPost]
[AllowAuthenticated(false)]
[ActionName("Same-name")]
public ActionResult AnonAction(ModelData data) { ... }
[HttpGet]
[Authorize]
[AllowAuthenticated(true)]
[ActionName("Same-name")]
public ActionResult AuthAction() { ... }
[HttpPost]
[Authorize]
[AllowAuthenticated(true)]
[ActionName("Same-name")]
public ActionResult AuthAction(OtherData data) { ... }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)