简体   繁体   English

如何生成一个 Url 到另一个 controller WebApi core 3.1

[英]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.

相关问题 .NET Core 3.1 WebApi - 来自另一个 WebApi 的直通 JSON 响应 - .NET Core 3.1 WebApi - Passthrough JSON response from another WebApi 如何在 .NET Core 3.1 WebAPI 中托管 Angular 应用程序? - How to host an Angular app inside .NET Core 3.1 WebAPI? WebApi .net core 3.1 - 如何自定义null发帖验证信息? - WebApi .net core 3.1 - How to customize validation message for null posting? 使用 ASP.NET Core 3.1,如何从页面 URL 中剥离主页 controller? - With ASP.NET Core 3.1, how can I strip the Home controller from a page URL? Asp.Net Core Url.Action使用WebApi控制器 - Asp.Net Core Url.Action use WebApi Controller 如何形成一个URL访问webapi controller? - How to form a URL to access the webapi controller? 在 Dotnet Core 3.1 中将数据从一个 controller 传递到另一个 - Passing data from one controller to another in Dotnet Core 3.1 如何在WebAPI Controller中从物理路径生成绝对路径 - How to generate absolute path from physical path in WebAPI Controller 如何在 asp.net core webapi 控制器中读取请求正文? - How to read request body in an asp.net core webapi controller? 如何在 .net core 3.1 中删除一个 Controller Action 的 CORS 限制 - How to remove CORS restriction for one Controller Action in .net core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM