gpt4 book ai didi

c# - WebApi AuthorizationFilterAttribute : ActionArguments are empty

转载 作者:太空狗 更新时间:2023-10-29 23:51:49 27 4
gpt4 key购买 nike

我正在为 WebApi Controller 实现 AuthorizationFilterAttribute,但我似乎无权访问传递到 Controller 的参数:

在 MVC4 中,这工作正常:

public class MyMVCController : Controller
{
[CanAccessMyResourceApi]
public MyViewModel Get(int id)
{
//...
}
}

public class CanAccessMyResourceMVCAttribute : CanAccessAttributeBase
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
var param = filterContext.Controller.ValueProvider.GetValue("id")
/// ...
}
}

但是在WebAPI中,我认为参数应该在ActionArguments中,但是这里的“param”是空的:

public class MyWebApiController : ApiController
{
[CanAccessMyResourceWebApi]
public MyViewModel Get(int id)
{
//...
}

}

public class CanAccessMyResourceWebApiAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext filterContext)
{
// the debugger shows that ActionArguments is empty:
var param = filterContext.ActionArguments["id"]
/// ...
}
}

传递到 Controller 的参数是否在其他地方可用? (当我删除过滤器属性时,我验证了 Controller 的操作是否正确获取了 Id 值。)

最佳答案

你看过this的解决方案了吗? StackOverflow 页面?

具体来说:

var variable = HttpContext.Current.Request.Params["parameterName"];

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var clientId = actionContext.ControllerContext.RouteData.Values["clientid"];

}
}

希望这对您有所帮助!

关于c# - WebApi AuthorizationFilterAttribute : ActionArguments are empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14736149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com