2

If this was a regular post of a form I could go to Request.Form['somevalue'] and get the value. If this was a get with a query string I could go to Request.QueryString["somevalue"] and get the value.

Where is the raw data when you post an ajax request. I need a value out of the raw data string in a filter method.

Any help will be appreciated!!

Edits below:

public class ValidateAntiForgeryId : FilterAttribute, IAuthorizationFilter {
    public void OnAuthorization(AuthorizationContext filterContext) {
        if (filterContext == null) {
            throw new ArgumentNullException("filterContext");
        }

        Guid filterGuid;
        Guid.TryParse(filterContext.RequestContext.HttpContext.Request.Form["__sessionId"], out filterGuid);

        if (filterGuid == Guid.Empty)
            throw new AuthenticationException("Authentication failure");
        try {
            var cookieGuid = (Guid)filterContext.RequestContext.HttpContext.Items["SessionId"];
        } catch {
            throw new AuthenticationException("Authentication failure");
        }
    }

The posted data looks like this:

{"SsnLastFour":"2222","AccountNumber":"B112233","__sessionId":"dca0a504-3c40-4118-ae19-afefb9bfc8bd"}

I need access to the __sessionId chunk inside the filter.

4

3 に答える 3

4

AJAX の投稿に魔法はありません。それらは単純な古い HTTP です。つまり、プレーンな古い HTTP ポスト値、および/またはプレーンな古い HTTP Get 値があることを意味します。

表示されない場合は、実際に送信していない可能性があります。

編集:

元の質問に含めなかった 2 つの問題: 1) これが JSON であること、および 2) これが (アクション メソッドではなく) AuthorizationFilter にあること。

どちらも答えを変えます。ASP.NET は JSON ポスト値をネイティブに理解しないため、Request.InputStream を介してそれらを解析する必要があります。MVC3 にはデフォルトで JSON モデル バインダーがありますが、モデル バインダーが実行される前に AuthorizationFilters が実行されるため、モデル バインダーが実行される前にアクセスすることになり、そのため FormsCollection は読み込まれません (Request.Form[] は機能しません)または、先ほど言ったように、asp.net は JSON をネイティブに理解していないためです)。

nuget を介して JSON.net をインストールすると、このタスクに役立つ場合があります。または、何を探しているかが正確にわかっているので、単純な解析ルーチンを作成することもできます。

于 2012-05-01T03:57:35.710 に答える
0

fitlerContext には、解析された JSON プロパティを持つ必要がある ActionParameters コレクションがあります (役立つ場合)。これは、InputStream を解析するよりも簡単かもしれません。

var sessionId = filterContext.ActionParameters["__sessionId"];
于 2012-07-12T20:07:29.163 に答える
0

パラメータ値は、通常の形式の投稿で受け入れるのと同じ方法で受け入れることができます。

元 :

$.get("User/Get", { userId : "24"} ,function(data){
  alert(data);
});

また

$("#yourDivId").load("User/Get?userId=23");

アクションメソッドは次のようになります

public ActionResult Get(int userId)
{
 // you have value present in userId
  if(Request.IsAjax())
  {
     return View("Partial/MyPartialView");
  }
  return View();
}

覚えておかなければならないことの 1 つは、アクション メソッドのパラメーター名は、パラメーター/クエリ文字列の名前と同じでなければならないということです。

于 2012-05-01T03:44:19.187 に答える