Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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
Asp.net mvc 从服务器导入较少的数据_Asp.net Mvc_Less_Dotless - Fatal编程技术网

Asp.net mvc 从服务器导入较少的数据

Asp.net mvc 从服务器导入较少的数据,asp.net-mvc,less,dotless,Asp.net Mvc,Less,Dotless,在我的ASP.NET MVC应用程序中,我有一个返回较少变量的操作 我想将这些变量导入我的主LESS文件 由于DotLess只导入扩展名为.less或.css的文件,因此建议采用什么方法进行此操作?我发现最简单的解决方案是实现IFileReader 下面的实现向前缀为“~/assets”的路径发出HTTP请求,否则我们将使用默认的文件读取器 请注意,这是原型代码: public class HttpFileReader : IFileReader { private readonly F

在我的ASP.NET MVC应用程序中,我有一个返回较少变量的操作

我想将这些变量导入我的主LESS文件


由于DotLess只导入扩展名为.less或.css的文件,因此建议采用什么方法进行此操作?

我发现最简单的解决方案是实现
IFileReader

下面的实现向前缀为“~/assets”的路径发出HTTP请求,否则我们将使用默认的
文件读取器

请注意,这是原型代码:

public class HttpFileReader : IFileReader
{
    private readonly FileReader inner;

    public HttpFileReader(FileReader inner)
    {
        this.inner = inner;
    }

    public bool DoesFileExist(string fileName)
    {
        if (!fileName.StartsWith("~/assets"))
            return inner.DoesFileExist(fileName);

        using (var client = new CustomWebClient())
        {
            client.HeadOnly = true;
            try
            {
                client.DownloadString(ConvertToAbsoluteUrl(fileName));
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    public byte[] GetBinaryFileContents(string fileName)
    {
        throw new NotImplementedException();
    }

    public string GetFileContents(string fileName)
    {
        if (!fileName.StartsWith("~/assets"))
            return inner.GetFileContents(fileName);

        using (var client = new CustomWebClient())
        {
            try
            {
                var content = client.DownloadString(ConvertToAbsoluteUrl(fileName));
                return content;
            }
            catch
            {
                return null;
            }
        }
    }

    private static string ConvertToAbsoluteUrl(string virtualPath)
    {
        return new Uri(HttpContext.Current.Request.Url, 
            VirtualPathUtility.ToAbsolute(virtualPath)).AbsoluteUri;
    }

    private class CustomWebClient : WebClient
    {
        public bool HeadOnly { get; set; }
        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = base.GetWebRequest(address);
            if (HeadOnly && request.Method == "GET")
                request.Method = "HEAD";

            return request;
        }
    }
}
要注册读卡器,请在应用程序启动时执行以下操作:

var configuration = new WebConfigConfigurationLoader().GetConfiguration();
            configuration.LessSource = typeof(HttpFileReader);