gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:53:21 30 4
gpt4 key购买 nike

我正在使用 .net 核心 3.1。在 docker 的帮助下,我将代码上传到了 heroku。但是当我发出 Web 请求时,我的所有端点都出现 405 错误。 (我看不到错误的更多细节)
使用 Get :

http://xxxx.herokuapp.com/api/pqrs/test/1315315
从 Visual Studio 代码和本地从我的 IIS 一切正常。但是当我部署到我的服务器时会出现问题。
我究竟做错了什么?这是我的 的代码 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;
}
}

}
更新:
这是我的 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"]
我从未见过 CORS 问题或类似问题。只是 405错误。

最佳答案

错误代码 405 意味着 "Method not allowed" .请求被服务器阻止。
首先,我会分析问题是真的在托管端(heroku)还是在您的 docker 配置中。
您的应用程序在本地计算机上的 IIS 中运行这一事实是一个重要的先决条件,但并不意味着它也将在 docker 中运行。
所以我会继续你的 docker 文件:在你将它部署到你的主机 (heroku) 之前,在本地运行 docker 镜像。
一种方法是在 Visual Studio 中为您的项目启用它,另一种方法是使用 command line :

docker container run --name [container_name] [docker_image]
运行后,通过检查端口映射
docker ps
然后,尝试通过访问它 http://localhost:<add your port>/<add your path here> 或者,如果您使用的是 SSL/TLS https://localhost:<add your port>/<add your path here>如果您发现收到 405,您可以继续:
检查 docker 容器内 Web 服务器的配置,是否允许所需的 HTTP verbs (获取,放置,发布,删除)?这些在 RESTful API 中是必需的.在您的示例中,您只是在使用 GET - 所以请检查这个。
还要检查,如果有的话 CORS缺少设置。这也可以阻止请求。
也值得一读 Microsoft's documents about Containerized Apps了解 Visual Studio 如何为您的应用创建容器。请务必阅读有关 的部分启用 SSL 的 ASP.NET Core 应用 描述 Kestrel 的设置(您在 ASP.NET Core 世界中使用的 Web 服务器)。
如果您发现您的 docker 在本地运行良好,请在托管端继续:
是否有任何防火墙设置阻止了您的请求?检查您的主机“heroku”的防火墙规则。
如果您使用 SSL/TLS,则需要提供证书 - 如上面链接中所述。
从上面的链接引用:

"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.
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)
or in the user secrets json file"

关于c# - 部署 ASP.NET Core Docker 项目 - 收到 405 错误(在我的 IIS 本地,Web 请求有效)。如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63746338/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com