代码之家  ›  专栏  ›  技术社区  ›  yavg

当我在heroku中部署我的项目并投入生产时,我得到了一个405错误,在我的IIS中,我的web请求在本地工作(C#)

c#
  •  0
  • yavg  · 技术社区  · 6 年前

    我正在使用 . 在…的帮助下 码头工人 我把代码上传到 赫罗库 . 但是当我提出一个网络请求时,我得到一个 405 我的所有终结点都出错。(我看不到错误的更多细节)

    Get :

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

    从visualstudio代码和本地IIS,一切都正常。但当我部署到服务器时,问题就出现了。

    我做错什么了?这是我的密码 :

        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 错误。

    0 回复  |  直到 6 年前
        1
  •  3
  •   Matt    6 年前

    405 方法 "Method not allowed"

    首先,我要分析问题是在主机端(heroku)还是在docker配置中。

    所以我会继续你的docker文件: 将其部署到宿主(heroku),运行docker映像 .

    一种方法是在visualstudio中为您的项目启用它,另一种方法是使用 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-so检查这个。

    CORS 缺少设置。这也可以阻止请求。

    它也值得一读 Microsoft's documents about Containerized Apps 了解VisualStudio如何为应用程序创建容器。确保你读了关于 启用SSLASP.NET核心应用程序 描述的设置 Kestrel (您在中使用的web服务器)ASP.NET核心世界)。

    如果您发现docker在本地运行良好,请在宿主端继续:

    是否有防火墙设置阻止您的请求?检查防火墙规则的主人“heroku”了。 如果您使用的是SSL/TLS,那么您需要提供一个证书-如上面链接中所述。

    "ASP.NETCore在Https文件夹下查找与程序集名称匹配的证书,这就是为什么它被映射到该路径中的容器。
    证书路径和密码也可以使用环境变量(即ASPNETCORE\u Kestrel\u Certificates\u Default\u path和ASPNETCORE\u Kestrel\u Certificates\u Default\u password)来定义
    或者在用户机密json文件中”