Я сделал основной проект 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 CORE
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?}");
//});
}