次のコードで、ユーザーがアプリケーションに登録できるようにします。
[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) に「ステータス」のフィールドを含める必要がありますが、コントローラーに適合すると思われますか?
申し訳ありませんが、私はそこにたくさん尋ねました...これを正しく行う方法を理解しようとしています.