1

In an ASP.NET Core 2.2 appliation, I have a user page and I need to obtain on that page a password reset link

Bellow is my code, that does not seem to work enter image description here

the EditUser.cshtml code

<script>
    var getUserLink = function () {
        var actionLink = "@Url.Action("ResetPassword", user)";
        $.getJSON(actionLink, function (data) {
            $("#resetLink").html(data["resetLink"]);
        });
    };
</script>

the EditUser.chtml.cs code

public class EditUserModel : PageModel
{
   /// ...

    public async Task<JsonResult> ResetPassword(ApplicationUserModel user)
    {
        var appUser = new ApplicationUser
        {
            UserName = user.Email,
            Email = user.Email,
            DisplayName = user.Name,
            OrganizationID = user.OrganizationID,
            EmailConfirmed = true
        };
        var code = await userManager.GeneratePasswordResetTokenAsync(appUser);
        var callbackUrl = Url.Page("/Account/ResetPassword",
            pageHandler: null,
            values: new { code },
            protocol: Request.Scheme);
        return new JsonResult(new { resetLink = callbackUrl });
    }
}

I mean, when I click on the "Obtain" button from the cshtml page, the JSON is asked, but the code does not enter the ResetPasword function in the cshtml.cs file. Where is the problem?

4
  • Would you mind posting your code as text? If the images get deleted, your question becomes nigh on reproducable; screenreaders can hardly deal with images and even for the visually unimpared it is not really easy to read. Commented Nov 14, 2019 at 10:20
  • @Marco it's ready Commented Nov 14, 2019 at 10:20
  • This is Razor pages, right? Commented Nov 14, 2019 at 10:21
  • yes, I put some js script in the razor page Commented Nov 14, 2019 at 10:22

1 Answer 1

2

On razor pages, you use "Handlers" to invoke requests to the razor page you are currently on.

So if you want to issue a get request you have to rename your method to follow a certain pattern:

public class EditUserModel : PageModel
{
    public async Task<JsonResult> OnGetResetPasswordAsync(ApplicationUserModel user)
    {
        /* ...*/
    }
}

And your javascript now becomes the following:

var getUserLink = function () {
    var actionLink = "./?handler=ResetPassword";
    $.getJSON(actionLink, function (data) {
        $("#resetLink").html(data["resetLink"]);
    });
};

Analog to this: If you want to issue a POST request, you need to name your handler method OnPostFoo or OnPostFooAsync and similar for each of the remaining HttpVerbs

If you want to issue POST requests you have to do a bit more lifting. Read up on it here: https://www.thereformedprogrammer.net/asp-net-core-razor-pages-how-to-implement-ajax-requests/

Sign up to request clarification or add additional context in comments.

5 Comments

thanks a lot, very useful, but how should I pass the user then as param? can't I use @Url.Action("ResetPassword", user) as link?
I wouldn't pass the complete user object to a get request. I'd just go for the UserId, retrieve the user on the server and resolve it from there.
to fix, the correct link would be var actionLink = "?handler=ResetPassword";
I honestly don't know from the top of my head, what the correct syntax would be. Either './?handler=foo' or something along those lines. Shouldn't take too long to find out.
thank you @Marco, it worked, I have some exceptions in the code behind, but it enters the code on the server side now !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.