我有以下 API 控制器:
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 方法中返回 null。 (我基于获取器this https://stackoverflow.com/questions/5225551/reference-generic-url-parameter-in-authorizeattribute code).
在我看来,这应该有效,但我似乎无法做到这一点。关于我做错了什么以及我应该如何获取发布的参数有什么想法吗?
编辑:我尝试使用以下内容直接读取帖子数据:
using (StreamReader inputStream = new StreamReader(request.InputStream))
{
output = inputStream.ReadToEnd();
}
request.InputStream.Position = 0;
这让我得到了 JSON 格式的发布数据,然后我可以解析它,但我的调用却永远不会成功。我在响应中收到以下异常:
<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
and HttpPost
(它在使用时确实有效HttpGet
)。错误报告已提交。