2015-11-15 67 views
2

我想獲得用戶的HttpContext.Current.Request.AnonymousIDRazor,然後將其發送到動作控制器以這樣的方式獲取AnonymousID在剃刀

<script> 
app.controller('loginController', function ($http, $scope, $location, $rootScope) { 
    $scope.login = function() { 

     //Send Anonymous to UserLogin action 
     $http.post("/Accounts/UserLogin?anonymous="+AnonymousId, $scope.model).success(function (response) { 
      $scope.message = response; 

      window.location = '/'; 
      $rootScope.ShowSpinner = false; 
     }); 
    } 
}); 
</script> 

有沒有辦法做到這一點?

+0

似乎'AnonymousID'是一個會話參數,那麼爲什麼你需要將它明確地發送回服務器?服務器已經知道了嗎? – masa

回答

0

在剃鬚刀,你可以有類似

@{ 
    // Just to make my expression smaller 
    var anonymousId = HttpContext.Current.Request.AnonymousID; 
} 
<script> 
    angular.module("someModule").value("anonymousId", "@anonymousId"); 
</script> 

注意注射剃刀僅僅是正常的。如果失敗,請嘗試打包(),如"@(anonymousId)"。唯一的技巧是我只是用引號將值包裝起來,以便它是JavaScript中的一個字符串。

然後你可以注入該數值到您的角度控制器:

<script> 
app.controller('loginController', 
     function ($http, $scope, $location, $rootScope, anonymousId) { 
      $scope.login = function() { 

      //Send Anonymous to UserLogin action 
      $http.post("/Accounts/UserLogin?anonymous="+anonymousId, $scope.model) 
       .success(function (response) { 
        ... 
      }); 
     } 
}); 
</script> 

當然,你也可以在控制器如直接寫...

.... 
    $http.post("/Accounts/UserLogin?anonymous=" 
       + "@(HttpContext.Current.Request.AnonymousID)", $scope.model). 
....