现在我们开始创建基于RazorPage的web应用。在解决方案中添加新的Asp.Net Core Web应用项目,名称为ZL.AbpNext.Poem.Web,然后安装volo.Abp.AspNetCore.Mvc和volo.Abp.Autofac,还要添加项目依赖ZL.AbpNext.Poem.Application和ZL.AbpNetx.Poem.EF:
接下来创建Abp模块:
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
using ZL.AbpNext.Poem.Application;
using ZL.AbpNext.Poem.Core;
using ZL.AbpNext.Poem.EF;
namespace ZL.AbpNext.Poem.Web
{
[DependsOn(typeof(AbpAspNetCoreMvcModule),
typeof(AbpAutofacModule),
typeof(PoemCoreModule),
typeof(PoemApplicationModule),
typeof(PoemDataModule))]
public class PoemWebModule : AbpModule
{
public override void OnApplicationInitialization(
ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseConfiguredEndpoints();
}
}
}
我们注意到原来Startup中的很多代码移动到模块中了,Startup变得很简单,只要启动模块就可以了:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ZL.AbpNext.Poem.Web
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddApplication<PoemWebModule>();
}
public void Configure(IApplicationBuilder app)
{
app.InitializeApplication();
}
}
}
最后,在Program中增加Autofac:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace ZL.AbpNext.Poem.Web
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseAutofac();
}
}
到这里,Abp相关的基础工作就完成了。我们还需要修改一下appsettings.json,将数据库连接加进去:
{
"ConnectionStrings": {
"Poem": "Server=localhost;Database=PoemNew;Trusted_Connection=True;MultipleActiveResultSets=true"
},
现在我们修改Index页面,显示数据中的分类列表。后台代码很简单,只要通过依赖注入获取IPoemAppService的实例,并调用相应的函数就可以了:
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using ZL.AbpNext.Poem.Application.Poems;
namespace ZL.AbpNext.Poem.Web.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private IPoemAppService service;
public List<CategoryDto> categories;
public IndexModel(ILogger<IndexModel> logger, IPoemAppService service)
{
_logger = logger;
this.service = service;
}
public void OnGet()
{
categories=service.GetAllCategories();
}
}
}
页面文件也很简单:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<ul>
@foreach (var category in Model.categories)
{
<li>@category.CategoryName</li>
}
</ul>
</div>
运行结果是这样的:
到这里,我们已经开发了一个简单的应用。