在 Controller 之外使用 LinkGenerator
Using LinkGenerator outside of Controller
在 ASP.NET Core 2.2 控制器上,我有以下内容:
var url = _linkGenerator.GetUriByAction(HttpContext, action: "GetContentByFileId", values: new { FileId = 1 });
我在控制器中注入了 LinkGenerator ...
现在我需要在不是控制器的 class 中生成 url,所以我尝试了:
var url = _linkGenerator.GetUriByAction(action: "GetContentByFileId", controller: "FileController", values: new { FileId = 1 });
但是,我收到消息说我需要更多参数。为什么?
在 Controller 之外使用 LinkGenerator 的正确方法是什么?
如果不使用需要 HttpContext
的 GetUriByAction
重载,那么您必须提供 all 其他
所需的参数
public static string GetUriByAction(
this LinkGenerator generator,
string action,
string controller,
object values,
string scheme,
HostString host,
PathString pathBase = default,
FragmentString fragment = default,
LinkOptions options = default)
在您的示例中是 scheme
和 host
。
作为替代方案,您还可以考虑注入 IHttpContextAccessor
,这样您就可以在控制器外部访问 HttpContext
,并且能够像从控制器内部调用时那样进行调用。
var url = _linkGenerator.GetUriByAction(_accessor.HttpContext,
action: "GetContentByFileId",
values: new { FileId = 1 }
);
我使用这个解决方案,对我来说效果很好。
首先:创建包含 IHttpContextAccessor 作为构造函数参数的自定义 class 服务。
通过 IHttpContextAccessor 可以获得 LinkGenerator 服务。
public class MyCustomClassService
{
LinkGenerator _LinkGenerator;
public MyCustomClassService(IHttpContextAccessor haccess)
{
_LinkGenerator = haccess.HttpContext.RequestServices.GetRequiredService<LinkGenerator>();
}
public LinkGenerator LinkGenerator
{
get
{
return _LinkGenerator;
}
}
}
然后:您需要将 MyCustomClassService 注册到 Startup class。
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<MyCustomClassService>();
}
}
在 ASP.NET Core 2.2 控制器上,我有以下内容:
var url = _linkGenerator.GetUriByAction(HttpContext, action: "GetContentByFileId", values: new { FileId = 1 });
我在控制器中注入了 LinkGenerator ...
现在我需要在不是控制器的 class 中生成 url,所以我尝试了:
var url = _linkGenerator.GetUriByAction(action: "GetContentByFileId", controller: "FileController", values: new { FileId = 1 });
但是,我收到消息说我需要更多参数。为什么?
在 Controller 之外使用 LinkGenerator 的正确方法是什么?
如果不使用需要 HttpContext
的 GetUriByAction
重载,那么您必须提供 all 其他
public static string GetUriByAction(
this LinkGenerator generator,
string action,
string controller,
object values,
string scheme,
HostString host,
PathString pathBase = default,
FragmentString fragment = default,
LinkOptions options = default)
在您的示例中是 scheme
和 host
。
作为替代方案,您还可以考虑注入 IHttpContextAccessor
,这样您就可以在控制器外部访问 HttpContext
,并且能够像从控制器内部调用时那样进行调用。
var url = _linkGenerator.GetUriByAction(_accessor.HttpContext,
action: "GetContentByFileId",
values: new { FileId = 1 }
);
我使用这个解决方案,对我来说效果很好。
首先:创建包含 IHttpContextAccessor 作为构造函数参数的自定义 class 服务。 通过 IHttpContextAccessor 可以获得 LinkGenerator 服务。
public class MyCustomClassService
{
LinkGenerator _LinkGenerator;
public MyCustomClassService(IHttpContextAccessor haccess)
{
_LinkGenerator = haccess.HttpContext.RequestServices.GetRequiredService<LinkGenerator>();
}
public LinkGenerator LinkGenerator
{
get
{
return _LinkGenerator;
}
}
}
然后:您需要将 MyCustomClassService 注册到 Startup class。
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<MyCustomClassService>();
}
}