0

I'm trying to generate a link to another controller, I was only able to create to the same controller.

This works in the same 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

[HttpGet("validate", Name = "Validate")]
public IActionResult ValidateNewUser(string token)
  {     
      return Ok();
  }
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
Nathiel Paulino
  • 534
  • 6
  • 17
  • You need to generate a URL pointing to another controller action method? Check this https://stackoverflow.com/questions/699782/creating-a-url-in-the-controller-net-mvc – Chetan Dec 11 '20 at 02:11

1 Answers1

0

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:

[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:

https://localhost:xxxx/Validation/Validate?token=token

enter image description here

Yiyi You
  • 16,875
  • 1
  • 10
  • 22