MVC阿比动作&安培; System.Web.Http.AuthorizeAttribute - 如何获得岗位参数? [英] MVC Api Action & System.Web.Http.AuthorizeAttribute - How to get post parameters?

查看:397
本文介绍了MVC阿比动作&安培; System.Web.Http.AuthorizeAttribute - 如何获得岗位参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下API控制器:

I have the following API Controller:

public class TestController : ApiController
{
    [HttpPost]
    [APIAuthorizeAttribute]
    public IQueryable<Computers> ListOfComputersInFolder(Guid folderId)
    {
        return GetListOfComputersForFolder(folderId);
    } // End of ListOfComputersInFolder
} // End of TestController 

和下面是我的基本 APIAuthorizeAttribute

public class APIAuthorizeAttribute : System.Web.Http.AuthorizeAttribute
{
    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        var Request = System.Web.HttpContext.Current.Request;
        var folderId = Request.RequestContext.RouteData.Values["folderId"] ?? Request.Params["folderId] as string;
        if(null == folderId)
        {
            folderId = actionContext.ControllerContext.RouteData.Values["folderId"];
        }

        base.OnAuthorization(actionContext);
    }
}

这是我遇到的问题是, folderId 即将空出来的onAuthorize方法。 (我依据一取出器href=\"http://stackoverflow.com/questions/5225551/reference-generic-url-parameter-in-authorizeattribute\">this code)。

The problem that I'm having is that folderId is coming out null in the onAuthorize method. (I based the fetcher on this code).

在我看来,这应该是工作,但我似乎无法得到它。什么我做错了,我应该如何去获得张贴的参数?任何想法

It seems to me that this should be working, but I cannot seem to get it to. Any ideas on what I am doing wrong and how I should go about getting the posted parameter?

编辑:我试着直接与下面的读取POST数据:

I tried reading the post data directly with the following:

using (StreamReader inputStream = new StreamReader(request.InputStream))
{
    output = inputStream.ReadToEnd();
}
request.InputStream.Position = 0;

这让我JSON格式的数据后,我可以再解析,但后来我的电话从来不使它虽然。我得到了响应以下异常:

Which gets me the post data in JSON format which I could then parse, but then my call never makes it though. I get the following exception in the Response:

  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.

at System.Json.JXmlToJsonValueConverter.JXMLToJsonValue(Stream jsonStream, Byte[] jsonBytes)\u000d\u000a   at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClass7.<OnReadFromStreamAsync>b__6()\u000d\u000a   at System.Net.Http.Internal.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}

编辑:
最后,好像这也可能会被使用 ApiController System.Web.Http.AuthorizeAttribute HttpPost (它使用时不工作 HTTPGET )。一个bug报告已提交。

In the end, it seems like this could possibly be a bug with the combination of ApiController, System.Web.Http.AuthorizeAttribute and HttpPost (it does work when using HttpGet). A bug report has been submitted.

推荐答案

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.onauthorization%28v=vs.98%29.aspx\">AuthorizeAttribute应该有一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizationcontext%28v=vs.98%29.aspx\">AuthorizationContext参数,而不是一个 HttpActionContext 之一,从您应该能够访问的RouteData 例如

The AuthorizeAttribute should have an AuthorizationContext parameter rather than a HttpActionContext one, from that you should be able to access the RouteData e.g.

public override void OnAuthorization(AuthorizationContext filterContext)
{
    var folderId = filterContext.RouteData.Values["folderId"];
    ...
}

更新

注意到您在使用 ApiController ,因此使用 Http.AuthorizeAttribute (解释了为什么没有一个 AuthorizationContext )。在这种情况下,你可以得到的RouteData 通过行动方面例如

Noticed you are using ApiController and as such using Http.AuthorizeAttribute (explains why you don't have an AuthorizationContext). In that case, you can get the RouteData via the action context e.g.

var folderId = actionContext.Request.GetRouteData().Values["folderId"];

这篇关于MVC阿比动作&安培; System.Web.Http.AuthorizeAttribute - 如何获得岗位参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆