当前位置: 首页 > news >正文

asp.net core mvc模块化开发

razor类库

在这里插入图片描述
在这里插入图片描述
新建PluginController

using Microsoft.AspNetCore.Mvc;

namespace RazorClassLibrary1.Controllers
{
    public class PluginController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Views下Plugin下新建Index.cshtml

@{
    ViewBag.Title = "插件页面";
}
<h1>我是插件页面</h1>

新建mvc
在这里插入图片描述
在这里插入图片描述
修改program.cs

using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.FileProviders;
using RazorClassLibrary1.Controllers;

namespace WebApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            var assembly = typeof(PluginController).Assembly;
            // Add services to the container.
            builder.Services.AddControllersWithViews()
                .AddApplicationPart(assembly)
                .AddRazorRuntimeCompilation();

            builder.Services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
            {
                options.FileProviders.Add(new EmbeddedFileProvider(assembly));
            });
            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();
        }
    }
}

运行项目
在这里插入图片描述
访问Plugin
在这里插入图片描述

普通类库

新建类库项目
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
修改csproj

<ItemGroup>
	<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Plugin2Controller.cs

using Microsoft.AspNetCore.Mvc;

namespace ClassLibrary1.Controllers
{
    public class Plugin2Controller : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

VIews\Plugin2\Index.cshtml

@{
    ViewBag.Title = "插件2页面";
}
<h1>我是插件2页面</h1>

Index.cshtml设置为嵌入资源
在这里插入图片描述
修改Program.cs

using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.FileProviders;
using RazorClassLibrary1.Controllers;
using ClassLibrary1.Controllers;
using Microsoft.AspNetCore.Mvc.ApplicationParts;

namespace WebApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            var assembly = typeof(PluginController).Assembly;
            var assembly2 = typeof(Plugin2Controller).Assembly;
            var part1 = new AssemblyPart(assembly);
            var part2 = new AssemblyPart(assembly2);
            builder.Services.AddControllersWithViews()
                .ConfigureApplicationPartManager(apm =>
                {
                    apm.ApplicationParts.Add(part1);
                    apm.ApplicationParts.Add(part2);
                })
                //需要安装Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
                .AddRazorRuntimeCompilation();

            builder.Services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
            {
                options.FileProviders.Add(new EmbeddedFileProvider(assembly));
                options.FileProviders.Add(new EmbeddedFileProvider(assembly2));
            });
            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            app.Run();
        }
    }
}

访问Plugin2
在这里插入图片描述

如果遇到Cannot find compilation library location for package ‘System.Security.Cryptography.Pkcs‘错误需要升级System.Security.Cryptography.Pkcs

参考

https://www.cnblogs.com/readafterme/p/18470211
https://learn.microsoft.com/zh-cn/aspnet/core/mvc/advanced/app-parts?view=aspnetcore-8.0
https://gitee.com/urselect/urshop
https://blog.csdn.net/u011511086/article/details/144530878

相关文章:

  • java退出程序异常分类
  • Geoserver修行记--离了个大谱,Geoserver的图层搜索不能用
  • 移动wsl2的系统镜像
  • 混合精度-基于torch内部
  • 阿里qwen大模型AI智能分析实时对话生成病例的DEMO
  • 设计一套水产养殖系统
  • 安卓NAS,众乐影音APP,低成本打造全能家庭存储中心
  • 使用excel.EasyExcel实现导出有自定义样式模板的excel数据文件,粘贴即用!!!
  • Midjourney使用教程—2.作品修改
  • 从零开始学Seata:分布式事务的终极解决方案
  • mysql not in 查询不出来null的原因
  • 【视觉提示学习】3.21论文随想
  • 流程优化的可配置文档结构化系统设计
  • VLAN综合实验报告
  • Webrtc编译官方示例实现视频通话
  • doris:弹性扩缩容
  • 零基础搭建智能法律知识库!腾讯云HAI实战教程
  • GLB文件介绍
  • LeetCode146.LRU 缓存(哈希表+双向链表)
  • debug - 安装.msi时,为所有用户安装程序
  • 证监会发布《上市公司募集资金监管规则》,6月15日起施行
  • 商务部:中方敦促美方尽快停止232关税措施
  • 国防部:菲方应停止一切侵权挑衅危险举动,否则只会自食苦果
  • 公元1058年:柳永词为什么时好时坏?
  • 浙能集团原董事长童亚辉被查,还是杭州市书法家协会主席
  • 文化润疆|为新疆青少年提供科普大餐,“小小博物家(喀什版)”启动