如何操作将路由与多个文件夹链接起来,而不仅仅是控制器和操作?

问题描述

我知道我可以像这样链接一条路线:

 // 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();
    }

这样做:

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

但是如果我想操作链接这样的东西怎么办?

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

提前致谢!

解决方法

将路由名称添加到您的操作中:

 [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();
    }

并使用名称:

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

我使用 Visual Studio 对其进行了测试,并且可以正常工作。

顺便说一下,您可以通过 2 种方式调用此操作。

1.使用路由链接和路由名称

  1. 通过 url 使用另一个输入控件或 httpclient 或 ajax 或 postman .../api/..controllerName../..actionName../..route 值...