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

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


http://www.kler.cn/a/596547.html

相关文章:

  • 网络知识编-数据链路层(以太网 局域网通信 ARP协议 ARP 欺骗 DDos 攻击)
  • Linux系统管理与编程07:任务驱动综合应用
  • 蓝牙AOA定位技术:开启车辆精准定位的智能新时代
  • MySQL数据库精研之旅第二期:库操作的深度探索
  • python:AI+ music21 构建 LSTM 模型生成爵士风格音乐
  • 【Java篇】静动交融,内外有别:从静态方法到内部类的深度解析
  • 单表达式倒计时工具:datetime的极度优雅(DeepSeek)
  • C++异常处理完全指南:从原理到实战
  • vue3配置代理实现axios请求本地接口返回PG库数据【前后端实操】
  • 红宝书第八讲:箭头函数与高阶函数:厨房工具与智能菜谱的对比
  • 3月22日星期六今日早报简报微语报早读
  • java项目之基于ssm的游戏攻略网站(源码+文档)
  • RHCE 使用nginx搭建网站
  • CH32V208蓝牙内部带运放32位RISC-V工业级微控制器
  • 1.1 结构体与类对象在List中使用区别
  • 使用flask_restful快速构建接口
  • golang压力测试工具如hey或wrk使用介绍
  • 可编辑52页PPT | 智慧园区安全生产顶层设计方案
  • [项目]基于FreeRTOS的STM32四轴飞行器: 十一.MPU6050配置与读取
  • 数据结构之双向链表-初始化链表-头插法-遍历链表-获取尾部结点-尾插法-指定位置插入-删除节点-释放链表——完整代码