项目作者: kolesnikovav

项目描述 :
Asp Net Core Plugin for integrating SPA Application with Asp Net Core Project
高级语言: C#
项目地址: git://github.com/kolesnikovav/AspSpaService.git
创建时间: 2020-07-10T19:13:21Z
项目社区:https://github.com/kolesnikovav/AspSpaService

开源协议:MIT License

下载


AspSpaService


Nuget (with prereleases)


Nuget

The Asp Net Core plugin for integrating SPA application with Asp Net Core.
Simplify SPA site development
This plugin can be used with any web framework in same manner.

Usage

Install package via NuGet

  1. dotnet add package AspSpaService

Install all the dependencies defined in Your SPA application

  1. cd <SPA application path>
  2. yarn // or npm install or pnpm install

Change your Startup.cs configuration file as follows:

  1. using AspSpaService;
  2. public class Startup
  3. {
  4. public void ConfigureServices(IServiceCollection services)
  5. {
  6. // ---- Your code -----------//
  7. services.AddNodeRunner();
  8. }
  9. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
  10. {
  11. // ---- Your code -----------//
  12. //this block starts vue spa application
  13. var wd = Directory.GetCurrentDirectory();
  14. var p = Path.Combine(wd,"samples", "hello-vite"); // path to your vuejs project
  15. app.UseSpa(
  16. spa => {
  17. spa.UseAspSpaDevelopmentServer(
  18. // for dispose nodejs process
  19. // after application close
  20. hostApplicationLifetime,
  21. // command for nodejs process
  22. // string
  23. "yarn",
  24. // argument for nodejs process
  25. // string
  26. "dev",
  27. // working directory
  28. // string
  29. p,
  30. // environment variables
  31. new Dictionary<string,string>(),
  32. // timeout for waiting node js process is ready to use
  33. TimeSpan.FromSeconds(15),
  34. // message when timeout has been exceeded
  35. // has defaul value = "Timeout has been exceeded" (can be ommited!)
  36. // string
  37. "Timeout has been exceeded",
  38. //logInformation for node js process
  39. // bool (true by default)
  40. true,
  41. //logError for node js process
  42. // some bundler emits many error strings during compilation
  43. // bool (false by default)
  44. false,
  45. //unsubscribeWhenReady
  46. // stop logging nodejs output when it ready to use
  47. // bool (true by default)
  48. true
  49. );
  50. }
  51. );
  52. }
  53. }

This library starts NodeJS process, and waiting for it emits line with served valid Uri

Sample configuratons

In folder sample/webapi has the web empty project that shows, how to use your favorite web framework with asp.
This code should be placed in Startup class in Configure method

Vue

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
  2. {
  3. // ---- Your code -----------//
  4. //this block starts vue spa application
  5. var wd = Directory.GetCurrentDirectory();
  6. var p = Path.Combine(wd,"samples", "hello-vue"); // path to your vuejs project
  7. app.UseSpa(
  8. spa => {
  9. spa.UseAspSpaDevelopmentServer(hostApplicationLifetime, "yarn", "serve", p, new Dictionary<string,string>(), TimeSpan.FromSeconds(15), "Timeout has been exceeded");
  10. }
  11. );
  12. }

Vite

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
  2. {
  3. // ---- Your code -----------//
  4. //this block starts vite spa application
  5. var wd = Directory.GetCurrentDirectory();
  6. var p = Path.Combine(wd,"samples", "hello-vite"); // path to your vitejs project
  7. app.UseSpa(
  8. spa => {
  9. spa.UseAspSpaDevelopmentServer(hostApplicationLifetime,"yarn", "dev", p, new Dictionary<string,string>(), TimeSpan.FromSeconds(15), "Timeout has been exceeded");
  10. }
  11. );
  12. }

Nuxt

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
  2. {
  3. // ---- Your code -----------//
  4. //this block starts nuxt spa application
  5. var wd = Directory.GetCurrentDirectory();
  6. var p = Path.Combine(wd,"samples", "hello-nuxt"); // path to your nuxt project
  7. app.UseSpa(
  8. spa => {
  9. spa.UseAspSpaDevelopmentServer(hostApplicationLifetime, "yarn", "dev", p, new Dictionary<string,string>(), TimeSpan.FromSeconds(15), "Timeout has been exceeded");
  10. }
  11. );
  12. }

React

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
  2. {
  3. // ---- Your code -----------//
  4. //this block starts react spa application
  5. var wd = Directory.GetCurrentDirectory();
  6. var p = Path.Combine(wd,"samples", "hello-react"); // path to your react project
  7. app.UseSpa(
  8. spa => {
  9. spa.UseAspSpaDevelopmentServer(hostApplicationLifetime,"yarn", "start", p, new Dictionary<string,string>(), TimeSpan.FromSeconds(15), "Timeout has been exceeded");
  10. }
  11. );
  12. }

Svelte

Note. Because Svelte and AspNetCore, by default, use the same port (5000), the port of Svelte should be changed!!!

  1. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime hostApplicationLifetime)
  2. {
  3. // ---- Your code -----------//
  4. //this block starts svelte spa application
  5. var wd = Directory.GetCurrentDirectory();
  6. var p = Path.Combine(wd,"samples", "hello-svelte"); // path to your svelte project
  7. app.UseSpa(
  8. spa => {
  9. spa.UseAspSpaDevelopmentServer(hostApplicationLifetime, "yarn", "dev", p, new Dictionary<string,string>(), TimeSpan.FromSeconds(15), "Timeout has been exceeded");
  10. }
  11. );
  12. }