简体   繁体   English

Asp.netCore GetWebResourceUrl

[英]Asp.netCore GetWebResourceUrl

Im trying to Build my own ScriptManagerController which will load JS files from another project. 我试图建立我自己的ScriptManagerController ,它将从另一个项目加载JS文件。

These files are saved as resource files. 这些文件保存为资源文件。

This is the code i used in Net451 这是我在Net451使用的代码

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

The problem is that NetAppCore 2.0 dose not have ClientScriptManager or Page which then i cant use GetWebResourceUrl 问题是NetAppCore 2.0不具有ClientScriptManagerPage ,然后我无法使用GetWebResourceUrl

I could still load the js file content and then load it throw HtmlString which in my case is really bad, my js file content is really big so i want to avoid it. 我仍然可以加载js文件内容然后加载它抛出HtmlString ,在我的情况下非常糟糕,我的js文件内容真的很大所以我想避免它。

Is there a workaround you could help me with. 有没有可以帮助我的解决方法。

Update 更新

Well this is what i did, I created a Controller that return a filestream in the other project and used MapRoute to mapp the namespace of the controller. 这就是我所做的,我创建了一个Controller,它在另一个项目中返回一个文件流,并使用MapRoute来映射控制器的命名空间。

If you have any other solution will still give you the points. 如果你有任何其他解决方案仍然会给你积分。

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

Follow steps 4, 5, and 6 of this post including-static-resources-in-razor-class-libraries 按照本文的第4,5和6步, 包括-static-resources-in-razor-classes-libraries

  1. Create a configuration file. 创建配置文件。

     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. (Optional) Create an extension class (you could also skip and use the services.ConfigureOptions line directly in the Startup class. (可选)创建扩展类(您也可以直接在Startup类中跳过并使用services.ConfigureOptions行。

      public static class EditorRCLServiceCollectionExtensions { public static void AddEditor(this IServiceCollection services) { services.ConfigureOptions(typeof(EditorRCLConfigureOptions)); } } 
  3. Add the new service to the startup class's ConfigureServices method: 将新服务添加到启动类的ConfigureServices方法:

     services.AddEditor(); 

Now you can use a file path just like a Content file, but for Embedded Resources! 现在您可以像Content文件一样使用文件路径,但是对于嵌入式资源!

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

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

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