Last post Nov 05, 2019 04:03 AM by bank5
Nov 03, 2019 02:12 PM|bank5|LINK
With MVC we can do something like this:
[Remote("IsUniqueEmail", "Home", HttpMethod = "POST", ErrorMessage = "An account already exists for this email address. Please sign in or reset your password.")]
Where IsUniqueEmail is the action and Home is the controller. Can we do something similar with Razor Pages? Or do I need to set up a controller and routing, just for remote validation?
Nov 03, 2019 10:14 PM|Mikesdotnetting|LINK
If you are using .NET Core 3.0, you can use the [PageRemote] attribute. It has the same parameters as the Remote attribute, except that it has PageName instead of controller, and PageHandler instead of action. Prior to that, you need to add a controller
and use the Remote attribute.
Nov 04, 2019 01:07 AM|bank5|LINK
Great thanks Mike! I changed it to:
[PageRemote(ErrorMessage = "Duplicate Email", HttpMethod = "Post", PageHandler = "OnCheckEmail", PageName = "/User/NewAccount")]
and my Handler is:
public JsonResult OnCheckEmail(string email)
return new JsonResult(false);
However, my browser console is giving me the message:
HTTP400: BAD REQUEST - The request could not be processed by the server due to invalid syntax.
(XHR)POST - https://localhost:44324/User/NewAccount?handler=OnCheckEmail
With MVC, I could simply return Json(false); but I'm guessing the syntax return new JasonResult(false); is causing the invalid syntax. Any ideas?
Nov 04, 2019 08:11 AM|Mikesdotnetting|LINK
AJAX Post requests usually result in a Bad Request status code in Razor Pages because you forgot to include a CSRF token in the request:
You need to add "__RequestVerificationToken" as an additional field.
Nov 04, 2019 04:38 PM|bank5|LINK
Thanks Mike. I added @Html.AntiForgeryToken() to my form and [ValidateAntiForgeryToken] to my PageModel. So "__RequestVerificationToken" does appear as an additional field.
The HTML for my email field looks good as far as I can tell:
<label class="control-label" for="User_Email">Email Address</label>
<input class="form-control" type="email" data-val="true" data-val-email="Please enter a valid email address" data-val-remote="Duplicate Email" data-val-remote-additionalfields="*.Email" data-val-remote-type="Post" data-val-remote-url="/User/NewAccount?handler=OnPostCheckEmail" data-val-required="Required Field" id="User_Email" name="User.Email" value="" />
<span class="text-danger field-validation-valid" data-valmsg-for="User.Email" data-valmsg-replace="true"></span>
However, I'm still getting the 400 error and don't see any network traffic. The only time I have been able to get it to trigger is if I put it in a separate page under OnGet(). I'm not too familiar with Ajax but will mark you answer as correct. Thanks
again and especially for creating learningrazorpages.com which I've referenced quite a bit.
Nov 04, 2019 09:01 PM|Mikesdotnetting|LINK
I added @Html.AntiForgeryToken() to my form and [ValidateAntiForgeryToken] to my PageModel.
One of the properties of the PageRemote attribute is called AdditionalFields. That's where you assign the hidden field containing the token:
[PageRemote(PageName="...", PageHandler="...", AdditionalFields="__RequestVerificationToken")]
Thanks again and especially for creating learningrazorpages.com which I've referenced quite a bit.
Nov 05, 2019 04:03 AM|bank5|LINK
Thanks again Mike. Finally got it working as intended. That was the most confusing thing I've run into so far with asp.net core so I'm sure a post about it would be super helpful, especially since there's limited info on it.
Two other things that really threw me off were:
I was originally passing my User object to the page and referencing the email property with User.Email. However, that would cause the 400 syntax error (I'm guessing it has something to do with the period). So I had to add a new property of Email to my
page model and reference it on the page with Email.
For some reason, I could only get it working with the OnPost page handler. If I changed it to say OnPost2, it wouldn't trigger.