我有以下代码,它适用于 /Projects/{userId}/{Id} 的 URL
但是如果我将值发布到页面上,其中 /Projects/ 和 JSON 值为
{"userId":1234,"Id":145}
它不会获取 JSON 值,UserId 和 Id 都为空。
代码如下
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
int documentIdfound, userIdFound;
int? documentId = null;
var documentIdRoute = httpContext.Request.RequestContext.RouteData.Values["Id"];
if (int.TryParse((string)documentIdRoute, out documentIdfound))
documentId = documentIdfound;
//if we havent found the ProjectId in the RouteData check the querystring. Just in case
if (documentId == null)
{
documentIdRoute = httpContext.Request.QueryString["Id"];
if (int.TryParse((string)documentIdRoute, out documentIdfound))
documentId = documentIdfound;
}
var userIdRoute = httpContext.Request.RequestContext.RouteData.Values["userId"];
int.TryParse((string)userIdRoute, out userIdFound);
var gi = GetCurrentUser();
try
{
var userFound = gi.UserProfiles.Where(x => x.UserId == userIdFound).FirstOrDefault();
if (userFound != null)
{
return gi.IsUserValid(userFound.UserId, documentId.Value);
}
else
return false;
}
catch (Exception ex)
{
AddErrorToDB(ex);
return false;
}
}