如何将ASP.NET MVC控制器操作的结果添加到捆绑包中? [英] How can I add the result of an ASP.NET MVC controller action to a Bundle?

查看:66
本文介绍了如何将ASP.NET MVC控制器操作的结果添加到捆绑包中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回Javascript文件的控制器操作.我可以从我的观点引用此文件,并且可以正常工作.我想将其与其他JS文件放在System.Web.Optimization.Bundle中.

I have a controller action that returns Javascript file. I can reference this file from my view and it works fine. I'd like to put it in a System.Web.Optimization.Bundle with the other JS files.

我正在尝试这样做,本质上是:

I'm trying to do this, essentially:

new Bundle().Include("~/DynamicScript/UrlDictionary");

我的Bundle中的其他文件可以很好地渲染,但是这个文件将被忽略.看到这种行为,我的假设是在应用程序能够通过路由基础结构解析URL之前就已经处理了绑定,或者绑定组件不会以允许解决该问题的方式来请求文件.

The other files in my Bundle get rendered just fine, but this one is ignored. Seeing this behavior, my assumption is that bundling gets handled before the application is able to resolve URLs through the routing infrastructure, or maybe that the bundling component doesn't request files in a manner that would allow that resolution to happen.

如果有人可以为我确认这一点和/或在这里为我指明正确的方向,将不胜感激.

If someone could confirm that for me and/or point me in a good direction here, it would be much appreciated.

推荐答案

我认为这是完全可行的-但在进入解决方案之前-请记住,该捆绑包是在首次点击时创建的,并且已被重用-这意味着任何动态"脚本仍必须是全局脚本(即它不能依赖于特定用户等).这就是为什么它通常只允许静态js文件的原因.这么说吧……我能想象出一种情况,您可能想要在您的js中粘贴版本号或类似的变量(尽管在这种情况下,我个人只是使​​用Ajax/JSON来获取它).

I think this is quite doable - but before I get into my solution - Remember that the bundle is created on the first hit and is reused - this means that any 'dynamic' script still has to be global (ie it cannot be dependant on a specific user etc). This is why generally it only allows static js files. Saying that... I could imagine a situation where you might want to stick variables like version number or something like that in your js (though in that case I personally would just use Ajax/JSON to get it).

我认为解决方法是从捆绑.在其中,您将覆盖EnumerateFiles方法.最初,您将枚举base.EnumerateFiles,然后包括您自己的虚拟文件.

I think the way to go about it is to create an derived type from Bundle. In it you would overwrite the EnumerateFiles method. Initially you would enumerate over the base.EnumerateFiles and then include your own virtual file.

类似(注意:未经测试的代码)

Something like (Note: not tested code):

public class VirtualMethodBundle : Bundle
{
    private List<VirtualFile> _virtualContent = new List<VirtualFile>();

    public override IEnumerable<VirtualFile> EnumerateFiles(BundleContext context)
    {
        foreach(var file in base.EnumerateFiles(context))
        {
            yield return file;
        }

        foreach(var virtual in _virtualContent)
        {
            yield return virtual;
        }
    }

    public void AddCustomFile(VirtualFile file)
    {
        _virtualContent.Add(method);
    }
}

然后,您将拥有一个特殊的 VirtualFile 定义的类型会覆盖Open/Name方法,并在此处返回动态内容……类似.

Then you would have a special VirtualFile defined type that overrides the Open/Name method and returns your dynamic content there... Something like.

public class MethodBasedVirtualFile : VirtualFile
{
    private readonly Func<string> _contentFactory;
    private readonly string _path;

    public MethodBasedVirtualFile(string path, Func<string> contentFactory)
    {
        _path = path;
        _contentFactory = contentFactory;
    }

    public override string Name { get { return _path; } }

    public override Stream Open()
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(_contentFactory());
        writer.Flush();
        stream.Position = 0;
        return stream;
    }
}

那么,要使用它,您将拥有的一切...

So then to use it all you would have...

var bundle = new VirtualMethodBundle();
bundle.Include(... real files ...);
bundle.AddCustomFile(
    new MethodBasedVirtualFile("~/DynamicScript/UrlDictionary",
    ... the method that creates the content of that script...)
);

如果您很聪明,可以制作一个使用url路径并使用MVC在需要时自动获取内容的UrlVirtualFile.

If you were clever you could just make a UrlVirtualFile that takes the url path and uses MVC to automatically get the content when required.

这篇关于如何将ASP.NET MVC控制器操作的结果添加到捆绑包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆