C# LinkGenerator返回空Url,而Url.Action不返回';T为什么?

C# LinkGenerator返回空Url,而Url.Action不返回';T为什么?,c#,asp.net-core,asp.net-core-2.2,C#,Asp.net Core,Asp.net Core 2.2,在ASP.NET Core 2.2控制器上,我尝试以3种方式生成链接: var a = Url.Action(action: "GetContentByFileId", values: new { fileId = 1 }); var b = _linkGenerator.GetUriByAction(HttpContext, action: "GetContentByFileId", controller: "FileController", values: new { fileId = 1

在ASP.NET Core 2.2控制器上,我尝试以3种方式生成链接:

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

var b = _linkGenerator.GetUriByAction(HttpContext, action: "GetContentByFileId", controller: "FileController", values: new { fileId = 1 });

var c = _linkGenerator.GetUriByAction(_httpContextAccessor.HttpContext, action: "GetContentByFileId", controller: "FileController", values: new { fileId = 1 });
结果

  • 在“a”中,使用Url.Action我得到了正确的链接

  • 在“b”和“c”中,我得到空值,并且我提供相同的数据。。。我想

我正在控制器中注入LinkGenerator,它不为空

我还在注入HttpContextAccessor,启动时:

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddApiVersioning(x => {
  x.ApiVersionSelector = new CurrentImplementationApiVersionSelector(x);
  x.AssumeDefaultVersionWhenUnspecified = true;
  x.DefaultApiVersion = new ApiVersion(1, 0);
  x.ReportApiVersions = false;
});
但如果我在启动时添加上一个代码行和以下内容:

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddApiVersioning(x => {
  x.ApiVersionSelector = new CurrentImplementationApiVersionSelector(x);
  x.AssumeDefaultVersionWhenUnspecified = true;
  x.DefaultApiVersion = new ApiVersion(1, 0);
  x.ReportApiVersions = false;
});
然后URL变为空

这个ApiVersion在文件之前添加了“v1.0”,因此它变成了“v1.0/files”

因此,链接生成器应该是:

var b = _linkGenerator.GetUriByAction(HttpContext, 
  action: "GetContentByFileId", 
  controller: "File", 
  values: new { apiVersion = "1.0", fileId = 1 
});
问题


有没有一种方法可以在不指定的情况下将apiVersion集成到LinkGenerator中?

问题是您使用的控制器名称带有
控制器
后缀。请从控制器名称中删除
Controller
后缀,并写下以下内容:

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

现在它应该可以工作了。

这看起来很有趣。不,它解决不了问题。。。我已经试过了,但我刚才又试了一次。“b”再次为空。和“a”使用Url。操作不是。我已经在使用它了!然后再次检查。它工作得很好。@MiguelMoura我还检查过,只有当
ControllerName
带有
Controller
后缀或未找到操作名称时,它才会返回null。因此,请在
b
c
@TanvirArjel中再次检查您的代码。我尝试了您的代码,但这不是唯一的问题。。。我刚刚更新了问题。我现在知道怎么解决了。。。不确定是否有更好的方法。。。有什么建议吗?替换应用程序中的默认MVC路由,以使用startup.cs中configure方法中的Endpoints路由。
         app.UseEndpoints(endpoints =>
          {
            // Default route
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Account}/{action=Login}/{id?}");
           });

services.AddMvc(options => options.EnableEndpointRouting = true);

      string url = _generator.GetUriByAction("index", "home", null, 
 _accessor.HttpContext.Request.Scheme, _accessor.HttpContext.Request.Host);
        var url1 = _generator.GetPathByAction("index", "home", 
   new { FileId = 1 });