1

I know I can action link a route like this:

 // GET: Home
    [Route("Home/About/{text}/{a}/{b?}/{c?}")]
    public ActionResult About(string text, string a, string b, string c)
    {
      ViewBag.Message = text + a + b + c;
      return View();
    }

by doing this:

@Html.ActionLink("GO", "About", "Home", new {text = "Test", a="value1",b="value2",c="value3" },null)

but what if I want to action link something like this?

 // GET: Home
    [Route("api/[controller]/[action]/{text}/{a}/{b?}/{c?}")]
    public ActionResult About(string text, string a, string b, string c)
    {
      ViewBag.Message = text + a + b + c;
      return View();
    }

Thanks in advance!

1 Answers1

0

Add route name to your action:

 [Route("api/[controller]/[action]/{text}/{a}/{b?}/{c?}", Name ="AboutRoute")]
    public ActionResult About(string text, string a, string b, string c)
    {
      ViewBag.Message = text + a + b + c;
      return View();
    }

and use the name :

@Html.RouteLink("GO","AboutRoute", new {
                        text = "Test", 
                        a="value1",
                           b="value2",
                       c="value3" } )

I tested it using Visual studio and it works properly.

And by the way you call this action by 2 ways.

1.Using Route Link and route name

  1. Using another input control or httpclient or ajax or postman by url .../api/..controllerName../..actionName../..route values...
Serge
  • 40,935
  • 4
  • 18
  • 45
  • yes I tried it, but then it sends me to "https://localhost:44394/Home/AboutRoute". And actually I want to go to https://localhost:44394/api/[controller]/[action], being by the way [controller] not equal to "Home". – Joseph Dellaware Feb 10 '21 at 22:07
  • It is not about what is your url looks like. It is about to call the right action of the right controller with the right route values. – Serge Feb 10 '21 at 22:33