我还进行了其他设置以使Web api正常工作吗?

问题描述 投票:0回答:1

我做了一个asp.net核心项目,并添加了一个控制器,该控制器如下所示。我安装了SPA主题,并且路由模块如下。我通过执行ng server运行此应用程序。

挑战:

  1. 当我将URL设为http://localhost:4200/api/test时,我没有得到值。我制作了一个Web api Controller,并按如下所示放置了测试方法,但我得到了cannot GET api/test

  2. 是因为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?}");
            //});
        }

angular asp.net-core asp.net-web-api angular8 angular-router
1个回答
0
投票

取消注释UseEndpoints并将其移至UseSpa之前。有关更多信息-> https://www.infoq.com/articles/spa-asp-dotnet-core-3

© www.soinside.com 2019 - 2024. All rights reserved.