你好,我採用了棱角分明2.然而,當我得到一個404錯誤讀取試圖路線從網頁不同的畫面:角2路由404錯誤
GET http://localhost:5000/map/1 404(未找到)
該應用程序在後端集成了ASP.NET Core。
兩個問題:
(1)我需要在MVC控制器,以及一個方法說像
[Route("api/map")]
public IActionResult Map()
{
return View();
}
(2)是否有什麼錯我的角度路由?
app.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { UniversalModule } from 'angular2-universal';
import { AppComponent } from './components/app/app.component'
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { MapComponent } from './components/map/map.component';
const appRoutes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'map/:id', component: MapComponent },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: '**', redirectTo: 'home' }
];
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
NavMenuComponent,
MapComponent,
FetchDataComponent,
HomeComponent
],
imports: [
UniversalModule,
RouterModule.forRoot(appRoutes)
]
})
export class AppModule {
}
map.component.ts
import { Component, OnInit } from '@angular/core';
import { MapService } from './../map.service';
@Component({
selector: 'map-root',
templateUrl: 'map.component.html',
providers: [MapService]
})
export class MapComponent implements OnInit {
constructor(public _mapService: MapService) { }
public images: [any];
ngOnInit() {
}
}
map.component.html
<h1>This is a test</h1>
的index.html
@{
ViewData["Title"] = "Home Page";
}
<base href="/" />
<app class="container" style="display: block;">Loading...</app>
<script src="~/dist/vendor.js" asp-append-version="true"></script>
@section scripts {
<script src="~/dist/main-client.js" asp-append-version="true"></script>
}
個
的web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
Configure your application settings in appsettings.json. Learn more at https://go.microsoft.com/fwlink/?LinkId=786380
-->
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
這非常有道理。我的 元素在佈局視圖中不是索引。但是,我已經實施了您的文檔中建議的代碼,並且仍然收到錯誤。我已將修改添加到我的帖子中。任何見解都會很棒。Thx –
RyeGuy
嗨RyeGuy,你有這個工作。將應用程序移動到服務器後,我面臨同樣的問題。它可以正常工作,如果我訪問index.html,就像這樣:'myserver.com/appVirtualDir/index.html'。如果我嘗試直接訪問路由('myserver.com/appVirtualDir/mainview'),它將顯示404。 – Deepak