繁体   English   中英

在部署时使用 .NET 核心调用 api Angular Spa 时出错

[英]Error calling api Angular Spa with .NET Core on deployment

我为我的 API 和 Angular 为我的前端创建了一个带有 .NET Core 的项目。 我使用 Visual Studio 创建了这个项目,并选中了这个选项: 创作截图 在我开发时,一切正常,但是当我在运行 Windows Server 2012 和 IIS 的部署机器上时,我的 api 调用不再工作。

这是我的 controller:

[Route("api/")]
[Authorize]
[ApiController]
public class RequestController : ControllerBase
{
[HttpPost]
[Route("EvalPassword")]
public async Task<DtoResponseEvalPassword> EvalPassword(DtoRequestEvalPassword request)
{
    return new DtoResponseEvalPassword()
    {
        Score = (int)await _passwordService.EvalPassword(request.Password, request.Lang)
    };
}

这是我的 Startup.cs:

using MaSecurite.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace MaSecurite
{
    public class Startup
    {
        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.Add(new ServiceDescriptor(typeof(IPasswordService), new PasswordService()));
            services.Add(new ServiceDescriptor(typeof(IPassphraseService), new PassphraseService()));
            services.AddControllersWithViews();
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }


        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");

                app.UseHsts();
            }

            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            };

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            if (!env.IsDevelopment())
            {
                app.UseSpaStaticFiles();
            }

            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
    }
}

这是我的 api 调用(EvalPassword)之一的示例:

constructor(private http: HttpClient, private translateService: TranslateService, @Inject('BASE_URL') public baseUrl: string) { }

evalPassword(password: string): any {
  const dto = new DtoRequestEvalPassword(password, this.translateService.currentLang);
  return this.http.post(this.baseUrl + 'api/EvalPassword', dto);
}

我觉得奇怪的是,开发中的一切工作都很好。 在产品中,“角度”效果很好,但是我返回的每个 API 调用:

POST https://localhost/api/EvalPassword 500 (Internal Server Error)

我通过在我的 IIS 服务器上启用 Windows Auth 解决了这个问题。 我以为它已经启用了。 :') 要查看我的错误是什么,我只需输入 Startup.cs:

app.UseDeveloperExceptionPage();

对于生产环境。

暂无
暂无

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

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