ASP.NET Core 轻量化开源论坛项目,ASP.NET Core Light forum NETCoreBBS

采用 ASP.NET Core + EF Core Sqlite + Bootstrap 开发。

GitHub: https://github.com/linezero/NETCoreBBS

开发

  1. git clone https://github.com/linezero/NETCoreBBS.git

  2. 使用 Visual Studio 2017 打开 NetCoreBBS.sln

  3. 点击 调试->开始调试 即可运行起来,或者直接点击工具栏上的NetCoreBBS即可。

注意:默认为80端口,可能会和本地端口冲突,可以到Program.cs 中更改 .UseUrls("http://*:80"),然后更改启动URL既可。

功能

  1. 节点功能

  2. 主题发布

  3. 主题回复

  4. 主题筛选

  5. 用户登录注册

  6. 主题置顶

  7. 后台管理

  8. 个人中心

技术点大合集

架构 Clean Architecture

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

 

 

1. Areas

重点代码:

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "areaRoute",
                    template: "{area:exists}/{controller}/{action}",
                    defaults: new { action = "Index" });
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

增加一个 areaRoute ,然后添加对应的Areas 文件夹,然后Areas里的控制器里加上  [Area("Admin")] 。

2. ViewComponents

在项目里的ViewComponents 文件夹,注意对应视图在 Views\Shared\Components 文件夹里。

3. Middleware

RequestIPMiddleware 记录ip及相关信息的中间件

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训 View Code

4. Identity

集成Identity ,扩展User表,自定义用户表。

权限策略

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

            services.AddAuthorization(options =>
            {
                options.AddPolicy(                    "Admin",
                    authBuilder =>
                    {
                        authBuilder.RequireClaim("Admin", "Allowed");
                    });
            });

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

注册登录密码复杂度

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

            services.AddIdentity<User, IdentityRole>(options =>
            {
                options.Password = new PasswordOptions() {
                    RequireNonAlphanumeric = false,
                    RequireUppercase=false
                };
            }).AddEntityFrameworkStores<DataContext>().AddDefaultTokenProviders();

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

 

5. EF Core

EF Core 采用Sqlite 数据库。

读取配置文件

services.AddDbContext<DataContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

使用代码初始化数据库

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

        private void InitializeNetCoreBBSDatabase(IServiceProvider serviceProvider)
        {            using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
            {                var db = serviceScope.ServiceProvider.GetService<DataContext>();
                db.Database.Migrate();                if (db.TopicNodes.Count() == 0)
                {
                    db.TopicNodes.AddRange(GetTopicNodes());
                    db.SaveChanges();
                }
            }
        }

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

项目分层 DataContext 在 Infrastructure,使用dotnet  ef 命令注意事项 

dotnet ef migrations add InitMigration --startup-project ../NetCoreBBS/NetCoreBBS.csproj

更新指定字段,不用先查询实体。

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

        public IActionResult EditSave(Topic topic)
        {
            _context.Attach(topic);
            _context.Entry(topic).Property(r => r.Title).IsModified = true;
            _context.Entry(topic).Property(r => r.Content).IsModified = true;
            _context.SaveChanges();            return RedirectToAction("Index");
        }

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

 

6. Configuration

读取链接字符串 Configuration.GetConnectionString("DefaultConnection")

7. Partial Views

_LoginPartial.cshtml 头部登录部分分布视图

_PagerPartial.cshtml 分页分布视图

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

@{    var pageindex = Convert.ToInt32(ViewBag.PageIndex);    var pagecount = Convert.ToInt32(ViewBag.PageCount);
    pagecount = pagecount == 0 ? 1 : pagecount;
    pageindex = pageindex > pagecount ? pagecount : pageindex;    var path = Context.Request.Path.Value;    var query = string.Empty;    var querys = Context.Request.Query;    foreach (var item in querys)
    {        if (!item.Key.Equals("page"))
        {
            query += $"{item.Key}={item.Value}&";
        }
    }
    query = query == string.Empty ? "?" : "?" + query;
    path += query;    var pagestart = pageindex - 2 > 0 ? pageindex - 2 : 1;    var pageend = pagestart + 5 >= pagecount ? pagecount : pagestart + 5;
}<ul class="pagination">
    <li class="prev previous_page @(pageindex == 1 ? "disabled" : "")">
        <a href="@(pageindex==1?"#":$"{path}page={pageindex - 1}")">&#8592; 上一页</a>
    </li>
    <li @(pageindex == 1 ? "class=active" : "")><a rel="start" href="@(path)page=1">1</a></li>
    @if (pagestart > 2)
    {        <li class="disabled"><a href="#">&hellip;</a></li>
    }
    @for (int i = pagestart; i < pageend; i++)
    {        if (i > 1)
        {            <li @(pageindex == i ? "class=active" : "")><a rel="next" href="@(path)page=@i">@i</a></li>
        }
    }
    @if (pageend < pagecount)
    {        <li class="disabled"><a href="#">&hellip;</a></li>
    }
    @if (pagecount > 1)
    {        <li @(pageindex == pagecount ? "class=active" : "")><a rel="end" href="@(path)page=@pagecount">@pagecount</a></li>
    }    <li class="next next_page @(pageindex==pagecount?"disabled":"")">
        <a rel="next" href="@(pageindex==pagecount?"#":$"{path}page={pageindex + 1}")">下一页 &#8594;</a>
    </li>
</ul>

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

写的不是很好,可以优化成TagHelper。

8. Injecting Services Into Views

@inject SignInManager<User> SignInManager

@inject 关键字

9. Dependency Injection and Controllers

public IActionResult Index([FromServices]IUserServices user)

FromServices 在指定Action注入,也可以使用构造函数注入。

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

        private ITopicRepository _topic;        private IRepository<TopicNode> _node;        public UserManager<User> UserManager { get; }        public HomeController(ITopicRepository topic, IRepository<TopicNode> node, UserManager<User> userManager)
        {
            _topic = topic;
            _node = node;
            UserManager = userManager;
        }

平面设计培训,网页设计培训,美工培训,游戏开发,动画培训

 

10.发布

之前写过对应的发布文章 ASP.NET Core 发布至Linux生产环境 Ubuntu 系统

由于project.json 改成csproj,发布有所变动。

默认发布还是相同 dotnet publish,自带运行时发布时更改csproj。

编辑 NetCoreBBS.csproj

<RuntimeIdentifiers>ubuntu.14.04-x64</RuntimeIdentifiers>

后续同样是 dotnet publish -r ubuntu.14.04-x64

注意这个节点,默认发布的,服务器也要安装相同版本的runtime。

<RuntimeFrameworkVersion>1.0.0</RuntimeFrameworkVersion>

 

代码里面还有一些大家可以自己去挖掘。

NETCoreBBS 在RC2 的时候就已经开始了,有很多人应该已经看过这个项目,这篇文章是让大家更清楚的了解这个项目。

 

ASP.NET Core跨平台开发从入门到实战》 京东 淘宝 亚马逊 当当

博客示例代码GitHub:https://github.com/linezero/Blog

http://www.cnblogs.com/linezero/p/NETCoreBBS.html

网友评论