Javascript 手动利用MVC脚本绑定

Javascript 手动利用MVC脚本绑定,javascript,asp.net-mvc-4,scriptbundle,Javascript,Asp.net Mvc 4,Scriptbundle,有没有办法以编程方式呈现脚本包 在MVC中使用脚本包非常好。喜欢。使用它 现在我正在编写一个使用自宿主WebApi的应用程序,我还想在其中创建一个脚本包。 也就是说,我在项目中有许多javascript文件,我希望缩小、合并这些文件,然后根据请求返回 我想这应该是可能的。但是,似乎找不到有关它的任何信息。不幸的是,ASP.NET绑定机制与ASP.NET上下文紧密耦合。在自主机中使用它需要一些额外的工作。例如,您将需要一个自定义虚拟路径提供程序,该提供程序将能够从指定位置读取文件: interna

有没有办法以编程方式呈现脚本包

在MVC中使用脚本包非常好。喜欢。使用它

现在我正在编写一个使用自宿主WebApi的应用程序,我还想在其中创建一个脚本包。 也就是说,我在项目中有许多javascript文件,我希望缩小、合并这些文件,然后根据请求返回


我想这应该是可能的。但是,似乎找不到有关它的任何信息。

不幸的是,ASP.NET绑定机制与ASP.NET上下文紧密耦合。在自主机中使用它需要一些额外的工作。例如,您将需要一个自定义虚拟路径提供程序,该提供程序将能够从指定位置读取文件:

internal class MyVirtualPathProvider : VirtualPathProvider
{
    private readonly string basePath;
    public MyVirtualPathProvider(string basePath)
    {
        this.basePath = basePath;
    }

    public override bool FileExists(string virtualPath)
    {
        return File.Exists(this.GetFileName(virtualPath));
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        return new MyVirtualFile(this.GetFileName(virtualPath));
    }

    private string GetFileName(string virtualPath)
    {
        return Path.Combine(this.basePath, virtualPath.Replace("~/", ""));
    }

    private class MyVirtualFile : VirtualFile
    {
        private readonly string path;
        public MyVirtualFile(string path)
            : base(path)
        {
            this.path = path;
        }

        public override Stream Open()
        {
            return File.OpenRead(this.path);
        }
    }
}
然后,您可以将bundle.config文件附加到控制台应用程序,在其中注册所有捆绑包文件:

<?xml version="1.0" encoding="utf-8" ?>
<bundles version="1.0">
  <scriptBundle path="~/bundles/myBundle">
    <include path="~/foo.js" />
    <include path="~/bar.js" />
  </scriptBundle>
</bundles>
最后,您可以拥有一些API控制器,它将为我们在
bundle.config
文件中定义的自定义bundle提供服务:

public class MyScriptsController: ApiController
{
    public HttpResponseMessage Get()
    {
        string bundlePath = "~/bundles/myBundle";
        var bundle = BundleTable.Bundles.GetBundleFor(bundlePath);
        var context = new BundleContext(new HttpContext(), BundleTable.Bundles, bundlePath);
        var response = bundle.GenerateBundleResponse(context);
        return new HttpResponseMessage()
        {
            Content = new StringContent(response.Content, Encoding.UTF8, "text/javascript"),
        };
    }

    private class HttpContext : HttpContextBase
    {
    }
}

在本例中,我省略了客户端缓存,为了使用相应的
缓存
头提供捆绑内容,您显然应该在控制器内部考虑客户端缓存。

Hrm…看起来比我希望的要复杂一些。不过谢谢你。我一定会试试的。
public class MyScriptsController: ApiController
{
    public HttpResponseMessage Get()
    {
        string bundlePath = "~/bundles/myBundle";
        var bundle = BundleTable.Bundles.GetBundleFor(bundlePath);
        var context = new BundleContext(new HttpContext(), BundleTable.Bundles, bundlePath);
        var response = bundle.GenerateBundleResponse(context);
        return new HttpResponseMessage()
        {
            Content = new StringContent(response.Content, Encoding.UTF8, "text/javascript"),
        };
    }

    private class HttpContext : HttpContextBase
    {
    }
}