2013-05-01 118 views
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; 
    } 
} 
+0

這應該是一個模型綁定問題結合JSON來 你需要添加一個costum Json的模型綁定該變量 – 2013-05-01 10:27:49

回答

0

嘗試發送參數值作爲字符串:

{'userId':'1234','Id':'145'}

這將解決這個問題。