【问题标题】:How to action link a route with several folders, not only controller and action?如何操作将路由与多个文件夹链接,而不仅仅是控制器和操作?
【发布时间】:2021-05-14 13:00:27
【问题描述】:

我知道我可以像这样操作链接路线:

 // 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 a, string b, string c)
    {
      ViewBag.Message = text + a + b + c;
      return View();
    }

提前致谢!

【问题讨论】:

    标签: c# asp.net-mvc model-view-controller routes html.actionlink


    【解决方案1】:

    为您的操作添加路线名称:

     [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 或邮递员 .../api/..controllerName../..actionName../..路由值...

    【讨论】:

    猜你喜欢
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    相关资源
    最近更新 更多