我是否设置了任何其他设置来使 Web api 正常工作?
Do I have set anything else to make Web api working?
我做了一个 asp.net 核心项目并添加了一个控制器,控制器如下所示。
我安装了一个SPA主题,路由模块如下。我通过执行 ng server
运行 这个应用程序。
挑战:
当我将 URL 设为 http://localhost:4200/api/test
时,我没有得到值。我制作了一个 Web api 控制器并按如下方式放置测试方法,但我得到 cannot GET api/test
.
是不是因为我设置的路由以外的所有路由都被SPA屏蔽了?那么有没有办法不阻止具有路由前缀api/
?因为我在 React 上看到了这样做。
app-routing.module
// Angular
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
// Components
import { BaseComponent } from './views/theme/base/base.component';
import { ErrorPageComponent } from './views/theme/content/error-page/error-page.component';
// Auth
import { AuthGuard } from './core/auth';
const routes: Routes = [
{ path: 'auth', loadChildren: () => import('app/views/pages/auth/auth.module').then(m => m.AuthModule)},
{
path: '',
component: BaseComponent,
canActivate: [AuthGuard],
children: [
{
path: 'dashboard',
loadChildren: () => import('app/views/pages/dashboard/dashboard.module').then(m => m.DashboardModule)
},
{
path: 'mail',
loadChildren: () => import('app/views/pages/apps/mail/mail.module').then(m => m.MailModule)
},
{
path: 'ecommerce',
loadChildren: () => import('app/views/pages/apps/e-commerce/e-commerce.module').then(m => m.ECommerceModule),
},
{
path: 'ngbootstrap',
loadChildren: () => import('app/views/pages/ngbootstrap/ngbootstrap.module').then(m => m.NgbootstrapModule)
},
{
path: 'material',
loadChildren: () => import('app/views/pages/material/material.module').then(m => m.MaterialModule)
},
{
path: 'user-management',
loadChildren: () => import('app/views/pages/user-management/user-management.module').then(m => m.UserManagementModule)
},
{
path: 'wizard',
loadChildren: () => import('app/views/pages/wizard/wizard.module').then(m => m.WizardModule)
},
{
path: 'builder',
loadChildren: () => import('app/views/theme/content/builder/builder.module').then(m => m.BuilderModule)
},
{
path: 'error/403',
component: ErrorPageComponent,
data: {
type: 'error-v6',
code: 403,
title: '403... Access forbidden',
desc: 'Looks like you don\'t have permission to access for requested page.<br> Please, contact administrator'
}
},
{path: 'error/:type', component: ErrorPageComponent},
{path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{path: '**', redirectTo: 'dashboard', pathMatch: 'full'}
]
},
{path: '**', redirectTo: 'error/403', pathMatch: 'full'},
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [RouterModule]
})
export class AppRoutingModule {
}
ASP.NET核心
public class LoginController : ControllerBase
{
[HttpGet]
[Route("api/test")]
public string Test()
{
return "test is working";
}
启动
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// services.AddDbContext<CDSPORTALContext>(options =>
//options.UseSqlServer(
// Configuration.GetConnectionString("DefaultConnection")));
//services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
// .AddEntityFrameworkStores<CDSPORTALContext>();
// In production, the Angular files will be served from this directory
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSpa(spa =>
{
spa.Options.SourcePath = "Client";
spa.UseAngularCliServer(npmScript: "start"); //<--THIS LINE
});
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller=Home}/{action=Index}/{id?}");
//});
}
取消注释 UseEndpoints 并将其移到 UseSpa 之前。
更多信息 -> https://www.infoq.com/articles/spa-asp-dotnet-core-3
我做了一个 asp.net 核心项目并添加了一个控制器,控制器如下所示。
我安装了一个SPA主题,路由模块如下。我通过执行 ng server
运行 这个应用程序。
挑战:
当我将 URL 设为
http://localhost:4200/api/test
时,我没有得到值。我制作了一个 Web api 控制器并按如下方式放置测试方法,但我得到cannot GET api/test
.是不是因为我设置的路由以外的所有路由都被SPA屏蔽了?那么有没有办法不阻止具有路由前缀
api/
?因为我在 React 上看到了这样做。
app-routing.module
// Angular
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
// Components
import { BaseComponent } from './views/theme/base/base.component';
import { ErrorPageComponent } from './views/theme/content/error-page/error-page.component';
// Auth
import { AuthGuard } from './core/auth';
const routes: Routes = [
{ path: 'auth', loadChildren: () => import('app/views/pages/auth/auth.module').then(m => m.AuthModule)},
{
path: '',
component: BaseComponent,
canActivate: [AuthGuard],
children: [
{
path: 'dashboard',
loadChildren: () => import('app/views/pages/dashboard/dashboard.module').then(m => m.DashboardModule)
},
{
path: 'mail',
loadChildren: () => import('app/views/pages/apps/mail/mail.module').then(m => m.MailModule)
},
{
path: 'ecommerce',
loadChildren: () => import('app/views/pages/apps/e-commerce/e-commerce.module').then(m => m.ECommerceModule),
},
{
path: 'ngbootstrap',
loadChildren: () => import('app/views/pages/ngbootstrap/ngbootstrap.module').then(m => m.NgbootstrapModule)
},
{
path: 'material',
loadChildren: () => import('app/views/pages/material/material.module').then(m => m.MaterialModule)
},
{
path: 'user-management',
loadChildren: () => import('app/views/pages/user-management/user-management.module').then(m => m.UserManagementModule)
},
{
path: 'wizard',
loadChildren: () => import('app/views/pages/wizard/wizard.module').then(m => m.WizardModule)
},
{
path: 'builder',
loadChildren: () => import('app/views/theme/content/builder/builder.module').then(m => m.BuilderModule)
},
{
path: 'error/403',
component: ErrorPageComponent,
data: {
type: 'error-v6',
code: 403,
title: '403... Access forbidden',
desc: 'Looks like you don\'t have permission to access for requested page.<br> Please, contact administrator'
}
},
{path: 'error/:type', component: ErrorPageComponent},
{path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{path: '**', redirectTo: 'dashboard', pathMatch: 'full'}
]
},
{path: '**', redirectTo: 'error/403', pathMatch: 'full'},
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [RouterModule]
})
export class AppRoutingModule {
}
ASP.NET核心
public class LoginController : ControllerBase
{
[HttpGet]
[Route("api/test")]
public string Test()
{
return "test is working";
}
启动
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// services.AddDbContext<CDSPORTALContext>(options =>
//options.UseSqlServer(
// Configuration.GetConnectionString("DefaultConnection")));
//services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
// .AddEntityFrameworkStores<CDSPORTALContext>();
// In production, the Angular files will be served from this directory
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSpa(spa =>
{
spa.Options.SourcePath = "Client";
spa.UseAngularCliServer(npmScript: "start"); //<--THIS LINE
});
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller=Home}/{action=Index}/{id?}");
//});
}
取消注释 UseEndpoints 并将其移到 UseSpa 之前。
更多信息 -> https://www.infoq.com/articles/spa-asp-dotnet-core-3