微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 linx docker 容器上部署 aspnet 核心应用程序而不会出现 404 错误

如何解决如何在 linx docker 容器上部署 aspnet 核心应用程序而不会出现 404 错误

我有一个 ASPNET Core 5.0 web api 应用程序在 Windows 本地成功运行,但在部署到托管在 linux 服务器上的 linux docker 容器时失败。导航到 http://internal.apps.talquinelectric.com/is/api 时不会加载页面。我收到了 404 错误

launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json","iisSettings": {
    "windowsAuthentication": false,"anonymousAuthentication": true,"iisExpress": {
      "applicationUrl": "http://localhost:80","sslPort": 80
    }
  },"profiles": {
    "IIS Express": {
      "commandName": "IISExpress","launchbrowser": true,"launchUrl": "swagger","environmentvariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },"FixBatchAPI": {
      "commandName": "Project","launchUrl": "http://internal.apps.talquinelectric.com/is/api","applicationUrl": "http://internal.apps.talquinelectric.com/is/api","environmentvariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

Startup.cs

// 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();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json","FixBatchAPI v1"));
            }

            //app.UseHttpsRedirection();
                        
            app.UseRouting();

            app.UseAuthorization();

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

Dockerfile

# Syntax=docker/dockerfile:1
  FROM mcr.microsoft.com/dotnet/aspnet:5.0
  copY FixBatchAPI/net50/ App/
  workdir /App
  ENTRYPOINT ["dotnet","FixBatchAPI.dll"]

docker-compose.yml

version: "3"

services:
   fixbatch-api:
      image: "fixbatch-api-build:latest"
      container_name: "fixbatch-api"
      networks:
        - traefix_proxy
      labels:
        - "traefik.enable=true"
        - "traefik.docker.network=traefik_proxy"
 
        - "traefik.http.routers.fixbatch-api.entrypoint=https"
        - "traefik.http.routers.fixbatch-api.tls=true"
        - "traefik.http.routers.fixbatch-api.rule=Host(`internal.apps.talquinelectric.com`) && PathPrefix(`/is/api`)"
        - "traefik.http.services.fixbatch-api-service.loadbalancer.server.port=80"
        - "traefik.http.middlewares.fixbatch-api-middlewares.chain.middlewares=fixbatch-api-strip"
        - "traefik.http.middlewares.fixbatch-api-strip.stripprefix.prefixes=/is/api"

        - "traefik.http.routers.fixbatch-api-http.entrypoint=http"
        - "traefik.http.routers.fixbatch-api-http.rule=Host(`internal.apps.talquinelectric.com`) && PathPrefix(`/is/api`)"
        - "traefik.http.middlewares.fixbatch-api-redirect-https.redirectscheme.scheme=https"
        - "traefik.http.routers.fixbatch-api-http.middlewares=fixbatch-api-redirect-https"

        - "traefik.http.middlewares.cors.headers.accesscontrolallowmethods=GET,OPTIONS,PUT"
        - "traefik.http.middlewares.cors.headers.accesscontrolalloworigin=*"
        - "traefik.http.middlewares.cors.headers.addvaryheader=true"

networks:
   traefik_proxy:
      external: true
   default:
      external: false

docker-compose 日志的结果:

docker-compose-logs

我在浏览器中查看时得到的:

browser rendering of 404 response

解决方法

添加如下代码,即可打开swaggerui页面。

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json","FixBatchAPI v1"));
} 
// default,no below code
else {
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json","FixBatchAPI v1"));
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。