I am trying to implement AJAX in ASP.NET Core so I have created a new default "ASP.NET Core Web Application (.NET Core) with "Individual User Accounts".
我正在尝试在ASP中实现AJAX。NET Core因此我创建了一个新的默认的“ASP”。NET核心Web应用程序(。使用“个人用户帐户”。
I added "Microsoft.jQuery.Unobtrusive.Ajax": "3.2.3" at project.json under "dependencies" section.
我添加了“Microsoft.jQuery.Unobtrusive。Ajax”:“3.2.3”项目。json在“依赖性”一节。
for "Register.cshtml" I have changed the declaration to
“登记。我已将声明更改为
<form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" data-ajax="true" data-ajax-method="POST" class="form-horizontal">
at "Register" action in AccountController I have changed it to check if the post is using AJAX.
在AccountController的“Register”操作中,我更改了它以检查post是否使用了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);
}
The problem is that isAjax always returns false.
问题是isAjax总是返回false。
Thanks in Advanced for any help, AG
谢谢你的帮助,AG
1 个解决方案
#1
2
I have added "jquery-ajax-unobtrusive": "3.2.4" thru bower.json and then a reference to jquery.unobtrusive-ajax.js and now it is working. Thanks @jerome for your help
我添加了“jquery-ajax- un显眼”:“3.2.4”通过bower。json,然后引用jquery.unobtrusive-ajax。现在它开始工作了。谢谢@jerome的帮助
#1
2
I have added "jquery-ajax-unobtrusive": "3.2.4" thru bower.json and then a reference to jquery.unobtrusive-ajax.js and now it is working. Thanks @jerome for your help
我添加了“jquery-ajax- un显眼”:“3.2.4”通过bower。json,然后引用jquery.unobtrusive-ajax。现在它开始工作了。谢谢@jerome的帮助