c# - Asp.net Core 获取 WebResource Url

标签 c# asp.net-mvc .net-core

我正在尝试构建我自己的 ScriptManagerController,它将从另一个项目加载 JS 文件。

这些文件保存为资源文件。

这是我在Net451

中使用的代码
  var url=  Page.ClientScript.GetWebResourceUrl(this.GetType(), "namespace.CustomComboBox.js") + "?JSReloader=" + DateTime.Now.ToFileTime()
var sc= "<script src="+url+"></script>"

问题是 NetAppCore 2.0 没有 ClientScriptManagerPage,因此我无法使用 GetWebResourceUrl

我仍然可以加载 JS 文件内容然后加载它抛出 HtmlString 这在我的情况下真的很糟糕,我的 JS 文件内容真的很大所以我想避免它。

有解决办法吗?

更新

这就是我所做的,我在另一个项目中创建了一个返回文件流的 Controller ,并使用 MapRoute 来映射 Controller 的命名空间。

我对其他解决方案持开放态度。

  app.MapRoute(
            name: "xxx",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index" },
            namespaces: new string[] { "namespace" }

最佳答案

按照这篇文章的第 4、5 和 6 步操作 including-static-resources-in-razor-class-libraries

  1. 创建配置文件。

    internal class EditorRCLConfigureOptions : IPostConfigureOptions<StaticFileOptions>
    {
        private readonly IHostingEnvironment _environment;
    
        public EditorRCLConfigureOptions(IHostingEnvironment environment)
        {
            _environment = environment;
        }
    
        public void PostConfigure(string name, StaticFileOptions options)
        {
    
            // Basic initialization in case the options weren't initialized by any other component
            options.ContentTypeProvider = options.ContentTypeProvider ?? new FileExtensionContentTypeProvider();
    
            if (options.FileProvider == null && _environment.WebRootFileProvider == null)
            {
                throw new InvalidOperationException("Missing FileProvider.");
            }
    
            options.FileProvider = options.FileProvider ?? _environment.WebRootFileProvider;
    
    
            // Add our provider
            var filesProvider = new ManifestEmbeddedFileProvider(GetType().Assembly, "resources");
            options.FileProvider = new CompositeFileProvider(options.FileProvider, filesProvider);
        }
    }
    
  2. (可选)创建一个扩展类(您也可以跳过并直接在 services.ConfigureOptions 类中使用 Startup 行。

     public static class EditorRCLServiceCollectionExtensions
    {
        public static void AddEditor(this IServiceCollection services)
        {
            services.ConfigureOptions(typeof(EditorRCLConfigureOptions));
        }
    }
    
  3. 将新服务添加到启动类的 ConfigureServices方法:

    services.AddEditor();
    

现在您可以使用文件路径,就像 Content文件,但对于嵌入式资源!

<script src='@(pathToEmbeddedResource)' />

关于c# - Asp.net Core 获取 WebResource Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962473/

相关文章:

c# - Visual Studio 运行 .NET 应用程序中的调试日志文件位置

c# - 单个 C# 类中的静态成员初始化顺序

c# - 这两种暂停/恢复线程的方法有什么区别?

循环中的 C# List 循环?

c# - 只读静态字段如何为空?

asp.net-core - Visual Studio 2019 - IIS Express - 连接已重置 (ERR_CONNECTION_RESET)

c# - Assembly.GetExecutingAssembly() 和 typeof(program).Assembly 之间的区别

c# - asp.net mvc 2 - 模型绑定(bind)和选择列表

c# - 将数据类型 nvarchar 转换为 int 时出错 - 执行存储过程时

ubuntu - 如何使用 dotnet 将 .nupkg 推送到私有(private) NuGet 源?