0

次のコードで、ユーザーがアプリケーションに登録できるようにします。

        [System.Web.Http.HttpPost]
        [System.Web.Http.AllowAnonymous]
        //[ValidateAntiForgeryToken]
        //public HttpResponseMessage Register(RegisterModel model, string returnUrl)
        public UserProfileDto Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                if (WebSecurity.UserExists(model.UserName))
                {
                   throw new HttpResponseException(HttpStatusCode.Conflict);
                }
                else
                {
                    // Attempt to register the user
                    try
                    {
                        WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                        WebSecurity.Login(model.UserName, model.Password);

                        InitiateDatabaseForNewUser(model.UserName);

                        FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);

                        var responseMessage = new HttpResponseMessage(HttpStatusCode.Redirect);
                        responseMessage.Headers.Location = new Uri("http://www.google.com");

                        return _service.GetUserProfile(WebSecurity.CurrentUserId);
                    }
                    catch (MembershipCreateUserException e)
                    {
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }
                }
            }

            // If we got this far, something failed
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }

私の質問: これらの例外のいずれかに該当する場合、ユーザーに「そのユーザー名は既に存在します!」と伝えたいと思います。または「ええ、何かが起こりました。調査中です。」など。クライアント側では、これをどのように処理すればよいですか? ヘッダーのステータスを確認し、それに応じてビューに何かを送信するだけですか?

これは、考えられるエラーごとに異なるステータス コードを使用する必要があるということですか? それは間違っているように思えます...それは私に尋ねることにつながります-ある種のステータス関連のデータをクライアントに送り返す必要がありますか? その場合、戻り値の型 (この場合は UserProfileDto) に「ステータス」のフィールドを含める必要がありますが、コントローラーに適合すると思われますか?

申し訳ありませんが、私はそこにたくさん尋ねました...これを正しく行う方法を理解しようとしています.

4

2 に答える 2

1

ReasonPhraseは、エラーが発生した理由を読みやすく説明するために存在します。問題をエンドユーザーに伝えるのに単純なテキストによる説明では不十分な場合は、問題をユーザーに説明するための新しい標準的な方法がいくつかあります。

application/api-problem+json https://datatracker.ietf.org/doc/html/draft-nottingham-http-problem-03 application/api-problem+xml

application/vnd.error+json https://github.com/blongden/vnd.error application/vnd.error+xml

于 2013-04-05T12:47:59.117 に答える
0

登録情報またはエラー メッセージをクライアントに返すことができるモデルを使用します。

モデルは次のようになります。

public class RegistrationModel
{
    public UserProfileDto UserProfile { get; set; }
    public ErrorModel Error { get; set; }
}

public class ErrorModel 
{
    public string Message { get; set;}
}

また、おそらくモデルを指定できる HttpResponseMessage を返す必要があります。

return Request.CreateResponse<RegistrationModel>(HttpStatusCode.Created, MyModel);
于 2013-04-05T00:05:49.493 に答える