【问题标题】:Url.Action and LinkGenerator returns different URLsUrl.Action 和 LinkGenerator 返回不同的 URL
【发布时间】:2019-01-21 23:51:15
【问题描述】:

在 Asp.Net Core 2.2 控制器操作中,我有以下内容:

  var url1 = Url.Action(action: "GetContentByFileId", values: new { FileId = 1 });

  var url2 = _linkGenerator.GetUriByAction(HttpContext, action: "GetContentByFileId", values: new { FileId = 1 });

我在 URL1 中获得了正确的 url,但 URL2 为 null ...

我在 Controller 中注入 LinkGenerator 并且它不为空,所以我认为它可以...

知道我错过了什么吗?

【问题讨论】:

    标签: asp.net-core


    【解决方案1】:

    在第二个调用中包含预期的控制器。

    var url2 = _linkGenerator.GetUriByAction(HttpContext, 
        action: "GetContentByFileId",
        controller: "ControllerNameHere", //<--
        values: new { FileId = 1 }
    );
    

    第一个调用已经通过注入UrlHelper的当前ActionContext知道控制器

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2021-07-23
      • 1970-01-01
      • 2015-03-12
      相关资源
      最近更新 更多