簡體   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