[英]How to generate a Url to another controller WebApi core 3.1
I'm trying to generate a link to another controller, I was only able to create to the same controller.我正在尝试生成指向另一个 controller 的链接,我只能创建到相同的 controller。
This works in the same controller这适用于相同的 controller
string uri = Url.Link("T", new { token = "token" });
[Route("Taa", Name = "T")]
public IActionResult SomeMethod(string token)
{
return Ok();
}
I tried:我试过了:
Url.Link("Default", new { Controller = "Account", Action = "Login" });
Url.Route("Default", new { Controller = "Account", Action = "Login" }); (Route Does not exist)
The real Controller and Action are: Validation and Validate真正的 Controller 和 Action 是:Validation 和 Validate
[HttpGet("validate", Name = "Validate")]
public IActionResult ValidateNewUser(string token)
{
return Ok();
}
You can use您可以使用
Url.Link("Default", new { Controller = "Validation", Action = "Validate", token = "token" });
Here is a demo:这是一个演示:
startup(Configure):启动(配置):
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Controller: Controller:
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
public IActionResult Index()
{
var url = Url.Link("Default", new { Controller = "Validation", Action = "Validate", token = "token" });
return Ok();
}
}
url result: url 结果:
https://localhost:xxxx/Validation/Validate?token=token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.