ngx-admin with Asp.net Core 2.0, possibly plus OrchardCore

53 篇文章 0 订阅

refs:

https://www.cnblogs.com/dfun/p/8574662.html

 

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.

 

7

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值