问题
I am working on asp.net mvc application, The issue i am facing is that when a user clicks on the confirm your account link in his email, the action method "Account/ConfirmEmail" which take two parameters string userId and string code is not hit and i get a 404 "The resource cannot be found" instead.
url: http://company2.chamferedcube.local:64255/Account/ConfirmEmail?**userId**=7da7d8f5-2fb7-47b2-82fd-59e0fba81ffd&**code**=vJIT0%2BeNr9aQ5WU2q6d5vvJEYgfreq2u5l4b3M0OqoZBOQVrMBf9jxMVssaPu6PU71MIj2ufVNSq32LxxXDswJ%2BK%2B1GrD8A%2BLwsswlKYM72F4XIN0LC7PoBd5Xa0fXCz88wgnHKqxztpOlDZ7Bm%2FZjCdTx5KPJ64pLLgbcZBcQUyo70vV%2FW6hKELSz5NnPGS
i suspect that there is something in the string parameters that prevent the application from figuring out the route.
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return View("Error");
}
var result = await UserManager.ConfirmEmailAsync(userId, code);
return View(result.Succeeded ? "ConfirmEmail" : "Error");
}
what i have tried but without any luck is to define a route and assign an attribute to the same action method.
routes.MapRoute(
name: "emailConfirmation",
url: "{controller}/{action}/{userId}/{code}",
defaults: new { controller = "Account", action = "ConfirmEmail", userId = "", code="" },
namespaces: new string[] { "SaaS.Controllers" }
);
and the attribute
[Route("emailConfirmation")]
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
}
if i remove the userId and code parameters the action is hit with two null strings. any thoughts? Thanks
回答1:
You have the routes mapped as GET parameter in your query, but in the controller method they come inside the URL.
On top of that in the example provided you have ** wrapped around the URL variables. It should be sent with just the names if you were to use GET parameters.
Try your query like this: http://company2.chamferedcube.local:64255/Account/ConfirmEmail/7da7d8f5-2fb7-47b2-82fd-59e0fba81ffd/vJIT0%2BeNr9aQ5WU2q6d5vvJEYgfreq2u5l4b3M0OqoZBOQVrMBf9jxMVssaPu6PU71MIj2ufVNSq32LxxXDswJ%2BK%2B1GrD8A%2BLwsswlKYM72F4XIN0LC7PoBd5Xa0fXCz88wgnHKqxztpOlDZ7Bm%2FZjCdTx5KPJ64pLLgbcZBcQUyo70vV%2FW6hKELSz5NnPGS
You can see it's now /useridvalue/confirmcodevalue instead of GET parameters. The question mark is removed, the variable names are gone, and there's no question mark. This is how you have your route mapped in: url: "{controller}/{action}/{userId}/{code}",
EDIT:
In this case you can probably use Request.QueryString["userId"];
and Request.QueryString["code"];
to get the values directly. I'm more familiar with ASP.NET Core where query string parameters are automatically mapped to method parameters. You would have to remove the route mappign as well.
来源:https://stackoverflow.com/questions/62610838/confirmemail-action-method-returns-404