我已经为IUrlHelper创建了扩展方法。在扩展方法中使用.net核心依赖关系
public static class UrlHelperExtensions
{
public static string JavaScript(this IUrlHelper helper, string contentPath, IOptions<TypeScriptOptions> tsOptions)
{
if (tsOptions.Value != null && tsOptions.Value.Minify)
{
contentPath = Path.ChangeExtension(contentPath, ".min.js");
}
return helper.Content(contentPath);
}
public static string Css(this IUrlHelper helper, string contentPath, IOptions<LessOptions> lessOptions)
{
if (lessOptions.Value != null && lessOptions.Value.Minify)
{
contentPath = Path.ChangeExtension(contentPath, ".min.css");
}
return helper.Content(contentPath);
}
}
我想通过IOptions<TypeScriptOptions> tsOptions
和IOptions<LessOptions> lessOptions
使用.NET核心的依赖注入的方法。
在Razor视图我有以下几点:
@inject IOptions<CssOptions> lessOptions
<link href="@Url.Css("~/css/site.css", lessOptions)" rel="stylesheet" asp-append-version="true">
但我只想做:
<link href="@Url.Css("~/css/site.css")" rel="stylesheet" asp-append-version="true">
我试图寻找在.NET核心文档,我已经做了几次谷歌搜索,但我似乎无法找到一种方法来实现我想要的,而不诉诸于Tag Helpers,这不是我想做的事。
我该如何得到这个工作?
传递到方法?你想制作一个中间件吗?扩展方法是静态的,因此任何状态都必须是静态的或传入的。 – Romoku
我已更新我的问题以提供答案。 –