ngx-admin with Asp.net Core 2.0, possibly plus OrchardCore
1
Download ngx-admin from https://github.com/akveo/ngx-admin
2
Create a new Web Application in vs2017 through file->new->project->Select .Net Core->Select ASP.NET Core Web Application and Next->Select Empty Template(Not Angular) and OK
Now you have a .net core app and a ngx-admin app in different folders. Presume:
ngx-admin in D:\test\ngx-admin
asp.net application solution in D:\test\WebApplication1 and there's a project named WebApplication1 in D:\test\WebApplication1\WebApplication1
3
Copy all contents in D:\test\ngx-admin into D:\test\WebApplication1\WebApplication1. Yes only the contents, not the entire folder.
Don't panic. Although looks quite a mess in the .net core project folder now, you'll soon get along with it because your other modules of the .net core application don't have to stay in the same folder.
4
Open Webapplication1.sln using vs2017. Open Startup.cs and modify Configure method as below:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.Map("/admin", appNgxAdmin => {
appNgxAdmin.UseSpa(spa => {
spa.Options.SourcePath = "src";
if (env.IsDevelopment()) {
spa.UseAngularCliServer(npmScript: "start");
}
});
});
app.UseMvc();
}
Modify ConfigureServices method as below:
public void ConfigureServices(IServiceCollection services) {
services.AddMvc();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration => {
configuration.RootPath = "dist";
});
}
Now you'll see several errors. Install Microsoft.AspNetCore.SpaServices.Extensions package through nuget. Get rid of the rest errors by adding using Microsoft.AspNetCore.SpaServices.AngularCli;
5
Open package.json, modify these 2 lines:
"start": "ng serve --base-href=/admin/ --serve-path=/",
"build": "ng build --base-href=/admin/ --serve-path=/",
Save and right click package.json -> Restore Packges
Now try to run in vs2017 using IIS Express. Should show a blank page and if you go /admin , ngx-admin should already be working.
But there's a problem with some sockjs issue if you look at chrome dev-tool. I don't know why but could be an issue with Angular-cli earlier version which uses webpack-dev-server below v3. If you know how to solve it much appreciated if you can let me know.
Another issue is sometimes the browser complains 500 server error. Kindly let me know if you have the remedy.
It's normally not satisfying you yet. To make the whole thing working the backend controllers or alternatives are needed.
6
Notice we have services.AddMvc() and app.UseMvc() in place already which enables using controllers.
Create a Controllers folder under WebApplication1 project. Create a HomeController.cs class within it, with the content below:
using Microsoft.AspNetCore.Mvc; namespace WebApplication1.Controllers
{
[Route("api/[controller]")]
public class HomeController : Microsoft.AspNetCore.Mvc.Controller {
[HttpGet]
public IActionResult Get() {
return Ok("Hello WebApplication1!");
}
}
}
Now run the project and visit /api/home should show Hello WebApplication1! in the browser.
The very basic procedure is done. To work on both backend and frontend in a sigle IDE is the harmony.
For more info about how to setup MVC in Asp.Net Core, here's an official link to go: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup
The default Mvc setup only allows you to use controllers within the project. If you prefer putting your controllers in different modules and leave only the Spa app(s) here in the main project(the "host" app) Orchard Core provides utilities with full flexibility.
To use Orchard Core utilities, for the moment I'm writing this article, a myget.org source has to be configured to nuget in vs2017(by clicking the configure icon next to the package source selector): https://www.myget.org/F/orchardcore-preview/api/v3/index.json
For WebApplication1 project, install OrchardCore.Application.Mvc.Targets , OrchardCore.Module.Targets (edit: and OrchardCore.Mvc.Core) packages through myget. Don't forget to check "Including Pre-release" because the utilities are still in beta versions.
Modify Startup.cs -> Configure method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.Map("/admin", appNgxAdmin => {
appNgxAdmin.UseSpa(spa => {
spa.Options.SourcePath = "src";
if (env.IsDevelopment()) {
spa.UseAngularCliServer(npmScript: "start");
}
});
});
//app.UseMvc();
app.UseModules(); edit: later version UseOrchardCore();
}
Yes only the last line changed. Modify ConfigureService method:
public void ConfigureServices(IServiceCollection services) {
// services.AddMvc();
services.AddModules(); edit: later version services.AddOrchardCore().AddMvc();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration => {
configuration.RootPath = "dist";
});
}
Just the first line is changed.
Now create a .NET Standard Class Library(.NET Standard) project named MyControllers under your WebApplication1 solution.
Install OrchardCore.Application.Mvc.Targets and OrchardCore.Module.Targets packages for this project.
Create a class named MyController.cs under MyControllers project with the content:
using Microsoft.AspNetCore.Mvc; namespace MyControllers
{
[Route("api/[controller]")]
public class MyController : Microsoft.AspNetCore.Mvc.Controller {
[HttpGet]
public IActionResult Get() {
return Ok("Hello MyControllers!");
}
}
}
Add reference to MyControllers project in WebApplication1 project.
Run and visit /api/my you should see Hello MyControllers! in the browser.
That's it. Simple and neat. How to link ngx-admin with the api is out of scope here. Guess shouldn't be a big issue.
ngx-admin with Asp.net Core 2.0, possibly plus OrchardCore的更多相关文章
- asp.net core 2.0 web api基于JWT自定义策略授权
JWT(json web token)是一种基于json的身份验证机制,流程如下: 通过登录,来获取Token,再在之后每次请求的Header中追加Authorization为Token的凭据,服务端 ...
- 从头编写 asp.net core 2.0 web api 基础框架 (3)
第一部分:http://www.cnblogs.com/cgzl/p/7637250.html 第二部分:http://www.cnblogs.com/cgzl/p/7640077.html 之前我介 ...
- Ubuntu & Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践
相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...
- 【转载】从头编写 asp.net core 2.0 web api 基础框架 (3)
Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-template-from-scratc ...
- Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录
1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...
- Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级
1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...
- Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践
相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...
- Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了
Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...
- [Asp.net core 2.0]Ueditor 图片上传
摘要 在项目中要用到富文本编辑器,包含上传图片,插入视频等功能.但ueditor只有.net版本,没有支持core.那么上传等接口就需要自己实现了. 一个例子 首先去百度ueditor官网下载简化版的 ...
随机推荐
- S0.0 计算机如何看东西
标签(空格分隔):数字图像处理 opencv 当我们用特定软件打开一张图片或者更改某些位图的格式为txt时,就会发现图像的本质不过就是一堆数据罢了. 采样 我们可以用相机采样到一幅二维图像,图像的分辨 ...
- Linux epoll机制
epoll_create.epoll_ctl.epoll_wait.close 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是 ...
- JS_高程8.BOM window对象(1)
1.全局作用域 var age = 14; window.coloer = "pink"; console.log(delete window.age);//false 使用var ...
- 转 MYSQL InnoDB Record, Gap, and Next-Key Locks
http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html InnoDB has several types of re ...
- 数据库交互之减少IO次数
一个对程序有要求的人一定会尽量去想办法减少IO次数来减少响应时间,但是又不能一味地为了减少IO次数而一直占用连接池.数据库连接一开一关也是需要耗费时间的,下面以SqlServer为例列举几种常见的减少 ...
- 4、初识python
今天开始进行python的系统学习开始写随笔希望对看到的人有所帮助,写的不对的地方可以指出来大家共同进步. 预习: 1.安装python2和python3,实现多版本共存 2.用python语言编写代 ...
- __x__(38)0909第五天__雪碧图的制作
1. 用ps打开目标图片若干. 2. 调整合适的画布大小. 3. 将图片拖曳到一张里. 4. 存储为Web所用格式,选择 png24 .
- JavaScript学习day1
JavaScript 特点: javascript 是一种脚本语言,它的解释器被称为javascript引擎,JavaScript被发明用于在HTML网页上使用,给HTML 网页增加动态功能 由于ja ...
- Laravel项目部署上线(阿里云 Ubuntu 16.04)
第一次尝试把本地的项目上线, 购买了某云的轻量应用服务器, 镜像为Ubuntu 16.04 直接运行 apt-get install nginx 出错 根据提示运行 apt-get upda ...
- Linux下Python模式下【Tab】自动补全
注:此文为转载他人博客,如有侵权,请联系我删除 1.我们需要一个tab补全的功能脚本 #!/usr/bin/python # python tab file import sys import re ...