Ale*_*oma 4 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>"
Run Code Online (Sandbox Code Playgroud)
问题是NetAppCore 2.0没有ClientScriptManager或Page我不能使用GetWebResourceUrl
我仍然可以加载js文件的内容,然后加载它HtmlString,对于我来说这是非常糟糕的,我的js文件的内容确实很大,所以我想避免它。
有没有可以帮助我的解决方法。
更新资料
好吧,这就是我所做的,我创建了一个Controller,该Controller在另一个项目中返回文件流,并使用MapRoute映射了该控制器的名称空间。
如果您有任何其他解决方案,仍然会给您要点。
app.MapRoute(
name: "xxx",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index" },
namespaces: new string[] { "namespace" }
Run Code Online (Sandbox Code Playgroud)
请按照这篇文章的步骤4、5和6进行操作,包括在razor-class-libraries中的static-resources
创建一个配置文件。
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);
}
}
Run Code Online (Sandbox Code Playgroud)(可选)创建扩展类(您也可以跳过并services.ConfigureOptions直接在Startup类中使用该行。
public static class EditorRCLServiceCollectionExtensions
{
public static void AddEditor(this IServiceCollection services)
{
services.ConfigureOptions(typeof(EditorRCLConfigureOptions));
}
}
Run Code Online (Sandbox Code Playgroud)将新服务添加到启动类的ConfigureServices方法中:
services.AddEditor();
Run Code Online (Sandbox Code Playgroud)现在,您可以像使用文件一样使用文件路径Content,但可以使用嵌入式资源!
<script src='@(pathToEmbeddedResource)' />
| 归档时间: |
|
| 查看次数: |
99 次 |
| 最近记录: |