Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在扩展方法中使用.net核心依赖项_C#_.net Core - Fatal编程技术网

C# 在扩展方法中使用.net核心依赖项

C# 在扩展方法中使用.net核心依赖项,c#,.net-core,C#,.net Core,我已经为IUrlHelper创建了扩展方法 public static class UrlHelperExtensions { public static string JavaScript(this IUrlHelper helper, string contentPath, IOptions<TypeScriptOptions> tsOptions) { if (tsOptions.Value != null && tsOption

我已经为IUrlHelper创建了扩展方法

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);
    }
}
公共静态类UrlHelperExtensions
{
公共静态字符串JavaScript(此IUrlHelper帮助程序、字符串内容路径、IOptions选项)
{
if(tsOptions.Value!=null&&tsOptions.Value.Minify)
{
contentPath=Path.ChangeExtension(contentPath,.min.js);
}
返回helper.Content(contentPath);
}
公共静态字符串Css(此IUrlHelper帮助程序、字符串内容路径、IOptions lessOptions)
{
if(lessOptions.Value!=null&&lessOptions.Value.Minify)
{
contentPath=Path.ChangeExtension(contentPath,.min.css);
}
返回helper.Content(contentPath);
}
}
我想将
IOptions-tsOptions
IOptions-lessOptions
传递给使用.NET Core依赖项注入的方法

从剃须刀的角度来看,我有以下几点:

@inject IOptions<CssOptions> lessOptions
<link href="@Url.Css("~/css/site.css", lessOptions)" rel="stylesheet" asp-append-version="true">
@injectioptions lessOptions
但我只想做:

<link href="@Url.Css("~/css/site.css")" rel="stylesheet" asp-append-version="true">

我试过查看.NET核心文档,也做过一些谷歌搜索,但我似乎找不到一种方法来实现我想要的,而不求助于标签助手,这不是我想做的


我怎样才能让它工作呢?

正如@Romoku所说,扩展方法是静态方法,只将状态作为参数(或从静态)

你要么需要继续使用你的策略,把它作为一个论点来传递。或者放弃扩展方法的想法,创建一些通过DI解决的助手类或服务:

public class UrlHelperService
{
    private IOptions<CssOptions> _cssOptions;
    private IOptions<JavaScriptOptions> _jsOptions;
    private IUrlHelper _urlHelper;

    public UrlHelperService(
        IOptions<CssOptions> cssOptions,
        IOptions<JavaScriptOptions> jsOptions,
        IUrlHelper urlHelper)
    {
        _cssOptions = cssOptions;
        _jsOptions = jsOptions;
        _urlHelper = urlHelper;
    }

    public string JavaScript(string contentPath)
    {
        if (_jsOptions.Value != null && _jsOptions.Value.Minify)
        {
            contentPath = Path.ChangeExtension(contentPath, ".min.js");
        }
        return _urlHelper.Content(contentPath);
    }

    public string Css(string contentPath)
    {
        if (_cssOptions.Value != null && _cssOptions.Value.Minify)
        {
            contentPath = Path.ChangeExtension(contentPath, ".min.css");
        }
        return _urlHelper.Content(contentPath);
    }
}

传递到哪里的方法?你想做一个中间件吗?扩展方法是静态的,因此任何状态都必须是静态的或传入的。我更新了我的问题以提供答案。我认为使用服务的想法最有意义,因此我将使用该方法。
@inject UrlHelperService urlHelperService 
<link href="@urlHelperService.Css("~/css/site.css")" rel="stylesheet" asp-append-version="true">