本文介绍了Url.Page创建了错误的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这段代码:

  var callbackUrl = Url.Page("/Account/ConfirmEmail",null,新的{userId = user.Id,代码},Request.Scheme); 

结果是这样的:

注册簿来自哪里?为什么不是Account/ConfirmEmail?我确实在Account controller的Register方法中创建了这个函数,所以我想寄存器来自那里,但是为什么它使用了它而不是传入的?我确实看到它在最后添加了它作为参数,但是为什么呢?

据我所知,我做对了.

https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.mvc.urlhelperextensions.page?view=aspnetcore-2.1#Microsoft_AspNetCore_Mvc_UrlHelperExtensions_Page_Microsoft_AspNetCore_Mvc_IUrlHelper_System_String_System_String_System_Object_System_String 我仍在使用核心2.1.

他们的注册教程说的和我上面讲的一样:

https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&tabs=visual-studio#scaffold-register-login-and-logout

如果我使用 .Action 而不是 .Page ,则可以得到所需的结果,但是我想知道为什么 .Page 的行为不同

解决方案

它们都用于生成url,但是 Url.Action 用于MVC和 Url.Page 适用于Razor Pages.您在帐户控制器中使用需要 Url.Action 的代码.

请参考控制器/操作的网址生成页面的网址生成

在asp.net core 2.0中,scaffold Identity使用MVC,而在asp.net core 2.1+中,它使用Razor Pages.

Have a look at this code:

var callbackUrl = Url.Page("/Account/ConfirmEmail", null, new { userId = user.Id, code }, Request.Scheme);

The result is this:

Where does the Register come from? Why is it not Account/ConfirmEmail? I did create this in the Register method of Account controller, so I suppose register came from there, but why did it use that as opposed to what I passed in? I do see that it added it as a parameter at the very end, but why?

As far as I can tell, according to this, I did it right.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.urlhelperextensions.page?view=aspnetcore-2.1#Microsoft_AspNetCore_Mvc_UrlHelperExtensions_Page_Microsoft_AspNetCore_Mvc_IUrlHelper_System_String_System_String_System_Object_System_String_System_String_

I am using core 2.1 still.

EDIT: Their registration tutorial says the same thing as what I have above:

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&tabs=visual-studio#scaffold-register-login-and-logout

If I use .Action instead of .Page I get my desired result, but I would like to know why .Page acts differently.

解决方案

They are all used to generate url, however,Url.Action is for MVC and Url.Page is for Razor Pages.You use the code in Account controller which Url.Action is needed.

Refer to Url Generation for controller/action and Url Generation for pages

In asp.net core 2.0,scaffold Identity uses the MVC while in asp.net core 2.1+, it uses Razor Pages.

这篇关于Url.Page创建了错误的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:10