简体   繁体   English

Angular 4 CLI生成的项目,与Asp.Net Core问题集成

[英]Angular 4 cli generated project, integration with Asp.Net Core issue

I am facing problem integrating angular 4 cli project into Visual Studio 2017 with Asp.Net core project. 我面临将问题4 cli项目与Asp.Net核心项目集成到Visual Studio 2017中的问题。

Here are the steps that I have taken. 这是我已采取的步骤。

  1. Created a angular4 project with >ng new shoppingcart 使用> ng new shoppingcart创建了angular4项目
  2. Inside the shoppingcart folder, I have created a Dot net web api project using >dotnet new webapi 在shoppingcart文件夹中,我使用> dotnet new webapi创建了一个Dot net Web API项目。

  3. Opened the dot new project in Visual studio 2017, and changed the outDir entry in .angular-cli.json to "outDir": "wwwroot". 在Visual Studio 2017中打开了dot new项目,并将.angular-cli.json中的outDir条目更改为“ outDir”:“ wwwroot”。

  4. Added a service namely app.service.ts and invoked the web api controller. 添加了一个名为app.service.ts的服务,并调用了Web api控制器。

  5. Ran the application from visual studio and the browser opens. 从Visual Studio中运行该应用程序,然后浏览器打开。 It shows http://localhost:6353/ and error 404 is shown. 它显示http:// localhost:6353 /并显示错误404。

Now if I type " http://localhost:6353/values , then I get the values returned by values controller, which is understandable. 现在,如果我输入“ http:// localhost:6353 / values” ,那么我会得到由values控制器返回的值,这是可以理解的。

But, my problems are these: 但是,我的问题是这些:

a) It is supposed to show the index.html under src folder (by copying it to wwwroot folder), but it does not. a)应该在src文件夹下显示index.html(通过将其复制到wwwroot文件夹),但不显示。 As a result, entire typescript logic is not getting executed. 结果,整个打字稿逻辑没有得到执行。 b) the browser is supposed to show the same index .html, but it does not. b)应该使浏览器显示相同的索引.html,但不会。

Can you help me? 你能帮助我吗?

Here is my 这是我的

.angular-cli.json file. .angular-cli.json文件。

  {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
  "name": "shopping-cart"
 },
 "apps": [
  {
  "root": "src",
  "outDir": "wwwroot",
  "assets": [
    "assets",
    "favicon.ico"
  ],
  "index": "index.html",
  "main": "main.ts",
  "polyfills": "polyfills.ts",
  "test": "test.ts",
  "tsconfig": "tsconfig.app.json",
  "testTsconfig": "tsconfig.spec.json",
  "prefix": "app",
  "styles": [
    "styles.css"
  ],
  "scripts": [],
  "environmentSource": "environments/environment.ts",
  "environments": {
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
  }
 }
],
  "e2e": {
  "protractor": {
  "config": "./protractor.conf.js"
  }
 },
 "lint": [
{
  "project": "src/tsconfig.app.json",
  "exclude": "**/node_modules/**"
},
{
  "project": "src/tsconfig.spec.json",
  "exclude": "**/node_modules/**"
},
{
  "project": "e2e/tsconfig.e2e.json",
  "exclude": "**/node_modules/**"
}
],
"test": {
 "karma": {
  "config": "./karma.conf.js"
  }
 },
 "defaults": {
  "styleExt": "css",
  "component": {}
 }
}

my entire project is on github and the link is: angular4 and Dot net core shopping cart 我的整个项目都在github上,链接是: angular4和Dot net core购物车

I have follows this example 我已经按照这个例子

Source example 源示例

But The index page is not opening in the browser, when I run from Visual studio (or by Dotnet run ) . 但是,当我从Visual Studio运行(或通过Dotnet run)运行时,浏览器中没有打开索引页面。 So any help will be appreciated. 因此,任何帮助将不胜感激。

My project structure, as you can see,index.html is not copied, and files like main.js, polyfills.js and test.js are copied under src folder. 如您所见,我的项目结构没有被复制,而main.js,polyfills.js和test.js之类的文件被复制到src文件夹下。

项目结构

I have resolved the issue. 我已经解决了这个问题。 I changed add the below code in startup.cs in configuration method 我更改了在配置方法的startup.cs中添加以下代码

app.Use(async (context, next) =>
  {
    await next();
    if (context.Response.StatusCode == 404 &&
      !context.Request.Path.Value.StartsWith("/api/"))
    {
      context.Request.Path = "/index.html";
      await next();
    }
  });

Earlier, it was wwwroot/index.html . 之前是wwwroot / index.html。 Now everything works fine. 现在一切正常。 When I run the project thru dotnet run command all the angular 4 features are available. 当我通过dotnet run命令运行项目时,所有角度4功能均可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM