1
我有下面的代码,这与/项目/ {用户id}/{ID}MVC授权属性验证JSON值
的URL工作正常,但如果我博文的值到页面/项目/并且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;
}
}
这应该是一个模型绑定问题结合JSON来 你需要添加一个costum Json的模型绑定该变量 – 2013-05-01 10:27:49