[英]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不具有ClientScriptManager
或Page
,然后我無法使用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
創建配置文件。
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); } }
(可選)創建擴展類(您也可以直接在Startup
類中跳過並使用services.ConfigureOptions
行。
public static class EditorRCLServiceCollectionExtensions { public static void AddEditor(this IServiceCollection services) { services.ConfigureOptions(typeof(EditorRCLConfigureOptions)); } }
將新服務添加到啟動類的ConfigureServices
方法:
services.AddEditor();
現在您可以像Content
文件一樣使用文件路徑,但是對於嵌入式資源!
<script src='@(pathToEmbeddedResource)' />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.