0

I'm trying to use attribute routing. For various reasons, one of my Guid argments required for a password reset must not be in the queryString so I've had to put it in the path. That itself isn't a problem, except for the fact that I can't get the resetToken to populate in my controller - even when it matches the controller method based on on the Route attribute I have defined.

So given this URL:

http://example.com/Account/ResetPasswordBySMS/96b7ba88-65e0-4dbc-a012-e69545a29a55/?userid=9d394579-afbb-49c4-ba21-877f4dad91fa

...would not populate "resetToken" here? (it's always null)enter image description here

1 Answer 1

0

Change your action to this:

[Route("~/Account/ResetPasswordBySMS/{resetToken?}")]
public ActionResult ResetPasswordBySMS(string resetToken, [FromQuery] string userId, [FromQuery] string code)
{
 var guidResetToken= new Guid(resetToken);
}

I tested url http://localhost:55480/Account/ResetPasswordBySMS/96b7ba88-65e0-4dbc-a012-e69545a29a55?userid=9d394579-afbb-49c4-ba21-877f4dad91fa&code=fghjk in Postman. It is working.

And it is not a good idea to mix query string with routing.I would add userId and code to routing too:

[Route("~/Account/ResetPasswordBySMS/{resetToken?}/{userId?}/{code?}
public ActionResult ResetPasswordBySMS(string resetToken, string userId, string code)

I can see the url error - slash after reset token and before ? mark. This slash should be gone.

5
  • Check my updated answer pls. And could you put a code how are calling this url.
    – Serge
    Commented Dec 16, 2020 at 11:08
  • Unfortunately I can't use that approach. It has to be how I put it in my question. I can create the URL but then a 3rd party I have no control over, appends "?code=xxx&userId=yyy" so the URL has to be in that format.
    – NickG
    Commented Dec 16, 2020 at 13:54
  • I was trying to put the ID in the path because you can't have two querystrings, so I can't have /ResetPasswordBySMS/?resetToken=x?userId=y&code=z
    – NickG
    Commented Dec 16, 2020 at 13:55
  • You have to try url like this (don't put / after resetToken) : example.com/Account/ResetPasswordBySMS/…....
    – Serge
    Commented Dec 16, 2020 at 14:13
  • I updated my answer with a tested solution. It is working in my test environment.
    – Serge
    Commented Dec 16, 2020 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.