繁体   English   中英

Asp.netCore GetWebResourceUrl

[英]Asp.netCore GetWebResourceUrl

我试图建立我自己的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来映射控制器的命名空间。

如果你有任何其他解决方案仍然会给你积分。

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

按照本文的第4,5和6步, 包括-static-resources-in-razor-classes-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. (可选)创建扩展类(您也可以直接在Startup类中跳过并使用services.ConfigureOptions行。

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

     services.AddEditor(); 

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM