简体   繁体   English

部署 ASP.NET Core Docker 项目 - 收到 405 错误(在我的 IIS 本地,Web 请求有效)。 如何解决?

[英]Deploy ASP.NET Core Docker project - get a 405 error (locally in my IIS, web requests works). How to fix it?

I am using .net core 3.1 .我正在使用.net core 3.1 With the help of docker I uploaded my code to heroku .docker的帮助下,我将代码上传到了heroku but when i make a web request I get a 405 error with all my endpoints.但是当我发出 Web 请求时,我的所有端点都出现405错误。 (I cant see more details of the error) (我看不到错误的更多细节)

using Get :使用Get

http://xxxx.herokuapp.com/api/pqrs/test/1315315

from visual studio code and also locally from my IIS everything works fine.从 Visual Studio 代码和本地从我的 IIS 一切正常。 but the problem occurs when I deploy to my server.但是当我部署到我的服务器时会出现问题。

What am I doing wrong?我究竟做错了什么? this is the code of my controller :这是我的控制器的代码:

    using System;
    using System.Collections.Generic;
    using System.IdentityModel.Tokens.Jwt;
    using System.Linq;
    using System.Security.Claims;
    using System.Text;
    using System.Threading.Tasks;
    using apiPQR.Contexts;
    using apiPQR.Entities;
    using apiPQR.Models;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.IdentityModel.Tokens;
    namespace apiPQR.Controllers
    {
        [ApiController]
        [Route("api/[controller]")]
        public class Pqrs : Controller

        {
            private readonly AppDbContext context;
            private readonly IConfiguration configuration;
            public Pqrs(AppDbContext context, IConfiguration configuration)
            {
                this.context = context;
                this.configuration = configuration;
            }
            [HttpGet, Route("test/{id}")]
            public PQRS Get(int id)
            {
                var num_pqrs = context.PQRS.FirstOrDefault(p => p.num_solicitud==id);
                return num_pqrs;
            }
        }

    }

update:更新:

this is my Dockerfile这是我的Dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base

WORKDIR /app

EXPOSE 80

EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build

WORKDIR /src

COPY ["apiPQR.csproj", ""]

RUN dotnet restore "./apiPQR.csproj"

COPY . .

WORKDIR "/src/."

RUN dotnet build "apiPQR.csproj" -c Release -o /app/build

FROM build AS publish

RUN dotnet publish "apiPQR.csproj" -c Release -o /app/publish

FROM base AS final

WORKDIR /app

COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "apiPQR.dll"]

At no time have I seen CORS issues or anything like that.我从来没有看到过 CORS 问题或类似的问题。 just the 405 error.只是405错误。

The error code 405 means "Method not allowed" .错误代码405表示“方法不允许” The request is being blocked by the server.请求被服务器阻止。

First of all, I would analyze if the issue is really on the hosting side (heroku) or in your docker configuration.首先,我会分析问题是真的在托管端(heroku)还是在您的 docker 配置中。

The fact that your application runs in IIS on your local machine is an important prerequisite, but doesn't mean it will also run in docker.您的应用程序在本地计算机上的 IIS 中运行这一事实是一个重要的先决条件,但并不意味着它也将在 docker 中运行。 So I would continue with your docker file: Before you deploy it to your hoster (heroku), run the docker image locally .所以我会继续你的 docker 文件:你将它部署到你的主机(heroku)之前,在本地运行 docker 镜像。

One way is to enable it in Visual Studio for your project, another way is to use the command line :一种方法是在 Visual Studio 中为您的项目启用它,另一种方法是使用命令行

docker container run --name [container_name] [docker_image]

Once it is running, check the port mapping via运行后,通过检查端口映射

docker ps

Then, try to access it via然后,尝试通过访问它
http://localhost:<add your port>/<add your path here>
or, if you're using SSL/TLS或者,如果您使用的是 SSL/TLS
https://localhost:<add your port>/<add your path here>

If you find that you're getting a 405, you can proceed further:如果您发现收到 405,您可以继续:

Check the configuration of your web server inside the docker container, does it allow the required HTTP verbs (GET, PUT, POST, DELETE)?检查 docker 容器内 Web 服务器的配置,它是否允许所需的HTTP 动词(GET、PUT、POST、DELETE)? Those are required in a RESTful API .这些在RESTful API 中是必需的。 In your example, you're just using GET - so check for this one.在您的示例中,您只是在使用 GET - 所以请检查这个。

Also check, if any CORS settings are missing.还要检查是否缺少任何CORS设置。 That can also block the requests.这也可以阻止请求。

It is also worth reading Microsoft's documents about Containerized Apps to understand how Visual Studio is creating the container for your app.还值得阅读Microsoft 关于容器化应用程序的文档,以了解 Visual Studio 如何为您的应用程序创建容器。 Make sure you read the section about SSL-enabled ASP.NET Core apps describing the settings for Kestrel (the web server you're using in the ASP.NET Core world).请务必阅读有关启用 SSL 的 ASP.NET Core 应用程序的部分,其中描述了Kestrel (您在 ASP.NET Core 世界中使用的 Web 服务器)的设置。

If you found your docker is running fine locally, continue on the hosting side:如果您发现 docker 在本地运行良好,请在托管端继续:

Are there any firewall settings blocking your requests?是否有任何防火墙设置阻止了您的请求? Check the firewall rules of your hoster "heroku" too.检查您的主机“heroku”的防火墙规则。 If you're using SSL/TLS, then you need to provide a certificate - as described in the link above.如果您使用 SSL/TLS,则需要提供证书 - 如上面的链接所述。

Cited from the link above:从上面的链接引用:

"ASP.NET Core looks for a certificate that matches the assembly name under the Https folder, which is why it is mapped to the container in that path. “ASP.NET Core 会在 Https 文件夹下查找与程序集名称匹配的证书,这就是将其映射到该路径中的容器的原因。
The certificate path and password can alternatively be defined using environment variables (that is, ASPNETCORE_Kestrel__Certificates__Default__Path and ASPNETCORE_Kestrel__Certificates__Default__Password)证书路径和密码也可以使用环境变量定义(即 ASPNETCORE_Kestrel__Certificates__Default__Path 和 ASPNETCORE_Kestrel__Certificates__Default__Password)
or in the user secrets json file"或在用户机密 json 文件中”

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

相关问题 ASP.NET Core 2.1 App在本地构建和运行成功,但无法通过Web Deploy发布到IIS - ASP.NET Core 2.1 App succeeds in Building and Running Locally but fails to Publish to IIS via Web Deploy ASP.NET 项目在 VS 运行时在本地运行,但不在本地计算机或服务器上的 IIS 上运行 - ASP.NET project works locally on build when VS is running but not on IIS on my local machine or my server 如何修复 IIS 上的 ASP.Net Core 404 错误? - How to fix ASP.Net Core 404 error on IIS? ASP.NET 核心 gRPC 在本地工作但不在 Docker - ASP.NET Core gRPC works locally but not in Docker ASP.NET 最小内核 6 web API 部署到 Z5DA5ACF461B4EFB7E26EC86106Z5B21 - ASP.NET Core 6 Minimal web API Deploy To IIS 如何修复asp.net核心mvc项目中的jquery错误? - how to fix jquery error in asp.net core mvc project? 将 ASP.NET Core API 部署到本地 IIS 错误 500 - Deploy ASP.NET Core API to Local IIS Error 500 如何在本地部署 Asp.Net Core 6 + Angular SPA - How deploy Asp.Net Core 6 + Angular SPA locally 如何将 ASP.NET 核心 web 应用程序部署到 ZAEA23489CE3AA9B6406EBB28E0CDA4016 上的 IIS 应用程序 - How to deploy ASP.NET core web application to IIS on Windows Server 2016 如何在本地IIS 7中运行asp.net项目 - How can I run asp.net project Locally IIS 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM