I am using .net core 3.1. With the help of docker I uploaded my code to heroku. but when i make a web request I get a 405 error with all my endpoints. (I cant see more details of the error)
using Get
:
http://xxxx.herokuapp.com/api/pqrs/test/1315315
from visual studio code and also locally from my IIS everything works fine. 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
#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. just the 405
error.
The error code 405 means "Method not allowed". 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.
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. So I would continue with your docker file: Before you deploy it to your hoster (heroku), run the docker image locally.
One way is to enable it in Visual Studio for your project, another way is to use the command line:
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 https://localhost:<add your port>/<add your path here>
If you find that you're getting a 405, you can proceed further:
Check the configuration of your web server inside the docker container, does it allow the required HTTP verbs (GET, PUT, POST, DELETE)? Those are required in a RESTful API. In your example, you're just using GET - so check for this one.
Also check, if any CORS settings are missing. 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. 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).
If you found your docker is running fine locally, continue on the hosting side:
Are there any firewall settings blocking your requests? Check the firewall rules of your hoster "heroku" too. If you're using SSL/TLS, then you need to provide a certificate - as described in the link above.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With