c# – 插件中的ASP .NET Core MVC 2.1 mvc视图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 插件中的ASP .NET Core MVC 2.1 mvc视图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6844字,纯文字阅读大概需要10分钟。
内容图文
![c# – 插件中的ASP .NET Core MVC 2.1 mvc视图](/upload/InfoBanner/zyjiaocheng/768/afbf630a282e4067801109db8e7b1172.jpg)
我有一个问题困扰了我好几天.
正如我在ASP .NET Core MVC中的插件中所做的那样,通过插件为我提供了视图
我有这种情况
解决方案:EVS
>控制器
> ……
>意见
> ……
>插件
>此文件夹包含插件dll
>其他文件夹……
> IPlugin.cs
> Program.cs
> Startup.cs
文件IPlugin.cs
...
using McMaster.NETCore.Plugins;
namespace EVS
{
public interface IPlugin
{
string Name { get; }
void Do();
void BootReg();
void BootExecute();
void PluginConfigure(IApplicationBuilder app, IHostingEnvironment env);
void PluginConfigureServices(IServiceCollection services);
}
public class PluginsManager
{
public List<PluginLoader> PluginLoaders;
public Dictionary<string, IPlugin> Plugins;
public Dictionary<string, Assembly> Views;
public List<String> dllFileNames;
public PluginsManager()
{
PluginLoaders = new List<PluginLoader>();
Plugins = new Dictionary<string, IPlugin>();
dllFileNames = new List<string>();
string PloginDirectory= Path.Combine(AppContext.BaseDirectory, "Plugins");
if (Directory.Exists(PloginDirectory))
dllFileNames.AddRange(Directory.GetFiles(PloginDirectory+"\\", "*.dll"));
foreach (string dllFile in dllFileNames)
{
if (!dllFile.Contains(".Views.dll"))
{
var loader = PluginLoader.CreateFromAssemblyFile(dllFile,sharedTypes: new[] { typeof(IPlugin) });
PluginLoaders.Add(loader);
}
else
{
//
//
}
}
foreach (var loader in PluginLoaders)
{
foreach (IPlugin plugin in loader.LoadDefaultAssembly().GetTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract).Select((x)=> (IPlugin)Activator.CreateInstance(x)))
if (!Plugins.ContainsKey(plugin.Name))
Plugins.Add(plugin.Name, plugin);
}
}
}
}
文件Program.cs
namespace EVS
{
public class Program
{
public static void Main(string[] args)
{
foreach (IPlugin plugin in Global.Static.PluginsManager.Plugins.Select((x) => x.Value))
plugin.BootReg();
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
文件Startup.cs
namespace EVS
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
foreach (IPlugin plugin in Global.Static.PluginsManager.Plugins.Select((x) => x.Value))
{
plugin.PluginConfigureServices(services);
}
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
foreach (IPlugin plugin in Global.Static.PluginsManager.Plugins.Select((x) => x.Value))
{
plugin.PluginConfigure(app,env);
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
现在让我们以插件为例
解决方案:EVS.TestPlugin
>控制器
> TestPluginController.cs
>意见
> test.cshtml
> TestPlugin.cs
file:TestPlugin.cs
namespace EVS.TestPlugin
{
internal class TestPlugin : IPlugin
{
public string Name
{
get
{
return "TestPlugin";
}
}
public void BootReg()
{
...
}
public void BootExecute()
{
...
}
public void Do()
{
}
public void PluginConfigure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc(routes =>
{
routes.MapRoute("TestPlugin", "TestPlugin/", new { controller = "TestPlugin", action = "index" });
routes.MapRoute("TestPlugint1", "TestPlugin/t1", new { controller = "TestPlugin", action = "t1" });
});
}
public void PluginConfigureServices(IServiceCollection services)
{
services.AddMvc().AddApplicationPart(typeof(TestPluginController).GetTypeInfo().Assembly).AddControllersAsServices();
}
}
}
file:Controllers / TestPluginController.cs
namespace EVS.TestPlugin.Controllers
{
public class TestPluginController : Controller
{
public IActionResult Index()
{
return Content("<h1>TestPluginController</h1>");
}
public IActionResult t1()
{
return View("test");
}
}
}
file:Views / test.cshtml
@{
Layout = null;
}
`.cshtml` test view
插件不包含在解决方案中,但它们是动态加载的,具体取决于它们是否在庄严的文件夹中.
问题:
因此我可以添加控制器
(EVS.TestPlugin.PluginConfigureServices(…))
和MapRoute(EVS.TestPlugin.PluginConfigure(…))
但是,我怎么能添加视图的上下文?
因此它将是:
?/ PluginName /浏览/ ViewName.cshatml
编辑
我发现了这个(link),但它并不是我真正需要的东西.它部分地解决了这个问题,因为它只在编译视图时才有效.
编辑2
我几乎解决了将插件添加到插件csproj上的问题,如下所示:
<ItemGroup>
<EmbeddedResource Include="Views\**\*.cshtml"/>
<Content Remove="Views\**\*.cshtml" />
</ItemGroup>
<PropertyGroup>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
</PropertyGroup>
在源项目上:
// p is a Instance of plugin
foreach(IPlugin p in Plugins.Select((x) => x.Value))
services.Configure<RazorViewEngineOptions>(options =>
{
options.FileProviders.Add(
new EmbeddedFileProvider(p.GetType().GetTypeInfo().Assembly));
});
这并没有解决我的问题,因为.cshtml文件在插件文件中有明确的内容,我需要的是能够从程序集pluginname.views.dll中添加视图或以其他方式,视图的重要性编译
编辑3
好消息,使用ILSpy我解析了一个PluginName.Views.dll文件,我发现它实现了RazorPage< object>
通过以下代码,我验证了我可以初始化cono RazorPage< object>的所有缓存.这允许我拥有创建视图的对象的实例
foreach (string dllview in Views.Select((x) => x.Key))
{
PluginLoader loader = PluginLoader.CreateFromAssemblyFile(dllview, sharedTypes: new[] { typeof(RazorPage<object>) });
foreach (RazorPage<object> RazorP in loader.LoadDefaultAssembly().GetTypes().Where(t => typeof(RazorPage<object>).IsAssignableFrom(t)).Select((x) => (RazorPage<object>)Activator.CreateInstance(x)))
{
// i need a code for add a RazorPagein the in RazorViewEngine, or anywhere else that allows register in the context
System.Diagnostics.Debug.WriteLine(RazorP.GetType().ToString());
/* - output
AspNetCore.Views_test
AspNetCore.Views_TestFolder_htmlpage
as you can see is the folder structure
- folder tree:
Project plugin folder
Views
test.cshtml
TestFolder
htmlpage.cshtml
*/
}
}
解决了
感谢我所解决的一切,似乎有一个问题
var assembly = ...;
services.AddMvc()
.AddApplicationPart(assembly)
有人甚至忘了把剃须刀放在工厂里
CompiledRazorAssemblyApplicationPartFactory
我解决了这个问题
services.AddMvc().ConfigureApplicationPartManager(apm =>
{
foreach (var b in new CompiledRazorAssemblyApplicationPartFactory().GetApplicationParts(AssemblyLoadContext.Default.LoadFromAssemblyPath(".../ViewAssembypath/file.Views.dll")))
apm.ApplicationParts.Add(b);
});
此外,我发布了一个有效地完成所有工作的库
解决方法:
您可以通过创建文件提供程序并将其添加到StartUp.ConfigureServices()方法中来添加自定义搜索路径,如下所示:
IFileProvider myFileProvider = new PhysicalFileProvider("C:/Some/File/Location");
services.AddMvc().AddRazorOptions(opt =>
{
opt.FileProviders.Add(myFileProvider);
});
内容总结
以上是互联网集市为您收集整理的c# – 插件中的ASP .NET Core MVC 2.1 mvc视图全部内容,希望文章能够帮你解决c# – 插件中的ASP .NET Core MVC 2.1 mvc视图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。