2

コメントセクションのあるページがあります。このセクションWebMethodは、新しいコメントを挿入するためにに連絡します。

[WebMethod]
public static bool insertComment(string commentString)
{
    //userName validation here
    string userName = (FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name);

    return new CommentClass().InsertComment(commentString, userName);
}

問題は、「非静的フィールドにはオブジェクト参照が必要です」です。隠しフィールドから情報を送信できることは知っていますdivが、その情報フィールドは簡単に変更される可能性があります。では、サーバー側で、どのユーザーが投稿しているかを知るためにどの方法を使用できるでしょうか。どうもありがとう!

4

1 に答える 1

2

Requestオブジェクトはに存在するインスタンスであるPageため、静的コンテキストでこのオブジェクトにアクセスするには参照が必要です。このコンテキストでHttpContext.Current.Requestにアクセスするために使用できます。Request

[WebMethod]
public static bool insertComment(string commentString)
{
    //userName validation here
    string userName = 
           (FormsAuthentication.Decrypt(
               HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name);
    return new CommentClass().InsertComment(commentString, userName);
}
于 2013-03-20T19:35:13.113 に答える