1
私はASP.NETコアにAJAXを実装しようとしているので、「個別のユーザーアカウント」を持つ新しいデフォルトの「ASP.NETコアWebアプリケーション(.NETコア)」を作成しました。ASP.NET Core AJAXが動作しませんか?
「Microsoft.jQuery。 Unobtrusive.Ajax ": "3.2.3依存関係" の下project.jsonで "Register.cshtml"。セクション
ための "AccountControllerにおける登録 "アクション" 私は、
<form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" data-ajax="true" data-ajax-method="POST" class="form-horizontal">
に宣言を変更しました"投稿がAJAXを使用しているかどうかを確認するために変更しました。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
var isAjax = HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest";
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
//var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
//var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
//await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
// $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
isAjaxは常にfalseを返します。
任意の助けのための高度のおかげで、 AG
なぜisAjaxが常にfalseであるのか不思議に思う前に、あなたのリクエストに「X-Requested-With」というヘッダーがあるかどうかを確認することから始めてください。私はあなたがしていないと確信しています –
あなたのproject.jsonにMicrosoft.jQuery.Unobtrusive.Ajaxを追加するだけであなたのプロジェクトにこのパッケージへの参照が追加されますが、あなたがそれを使用しているとは限りません。このライブラリを正しく使用する方法を学ぶ必要があります。それ以外の場合、本当にAJAXでHTTPリクエストを送信したい場合(理由はわかりません)、純粋なJavaScriptを使用するか、jQueryを使用してください(Unobtrusive.Ajax以外) –