ASP.NET Core MVC 3.1 项目开发记录
2021/8/29 17:36:07
本文主要是介绍ASP.NET Core MVC 3.1 项目开发记录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Autofac配置
ASP.NET Core提供依赖注入,但对于稍复杂一些的情况,用Autofac更友好。
Startup添加以下代码
1 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 2 { 3 …… 4 this.AutofacContainer = app.ApplicationServices.GetAutofacRoot(); 5 } 6 7 public ILifetimeScope AutofacContainer { get; private set; } 8 9 // ConfigureContainer is where you can register things directly 10 // with Autofac. This runs after ConfigureServices so the things 11 // here will override registrations made in ConfigureServices. 12 // Don't build the container; that gets done for you by the factory. 13 public void ConfigureContainer(ContainerBuilder builder) 14 { 15 // Register your own things directly with Autofac here. Don't 16 // call builder.Populate(), that happens in AutofacServiceProviderFactory 17 // for you. 18 builder.RegisterModule(new ServiceModule()); 19 } 20 21 class ServiceModule : Autofac.Module 22 { 23 protected override void Load(ContainerBuilder builder) 24 { 25 var bll = Assembly.Load(new AssemblyName("Square.Service")); 26 builder.RegisterAssemblyTypes(bll) 27 .Where(t => t.Name.EndsWith("Service")) 28 .AsImplementedInterfaces(); 29 } 30 }View Code
Program添加.UseServiceProviderFactory(new AutofacServiceProviderFactory())
Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
Areas配置
- 新建区域后要在Startup.Config内增加区域路由配置
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "System", pattern: "{area:exists}/{controller}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Login}/{action=Index}/{id?}"); });
2. 在控制器头上标记Areas特性
[Area("System")] public class RoleController : Controller
注意:Views等静态文件的Build Action应为Content
初始化数据库
- 创建数据库和架构(.NET Core CLI)
- cd DbContext所在项目
- dotnet ef migrations add InitialCreate
- dotnet ef database update
- 数据种子初始化数据(在本地调试,或引用dll完成,只在程序运行第一次时使用)
namespace MvcMovie.Models { public static class SeedData { public static void Initialize(IServiceProvider serviceProvider) { using (var context = new MvcMovieContext( serviceProvider.GetRequiredService< DbContextOptions<MvcMovieContext>>())) { // Look for any movies. if (context.Movie.Any()) { return; // DB has been seeded } context.Movie.AddRange( new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-2-12"), Genre = "Romantic Comedy", Price = 7.99M }, new Movie { Title = "Ghostbusters ", ReleaseDate = DateTime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M } ); context.SaveChanges(); } } } }View Code
public class Program { public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { SeedData.Initialize(services); } catch (Exception ex) { var logger = services.GetRequiredService<ILogger<Program>>(); logger.LogError(ex, "An error occurred seeding the DB."); } } host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }View Code
参考链接:
https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-mvc-app/working-with-sql?view=aspnetcore-5.0&tabs=visual-studio
https://docs.autofac.org/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting
这篇关于ASP.NET Core MVC 3.1 项目开发记录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#