0

My controller for one of my WebAPIs was working perfectly yesterday, but today I made some changes to projects outside of the actual controller code and now the API is not posting correctly. This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Markup;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace SanTool_WebAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class GeneratorStatusController : ControllerBase
    {
        static List<string> strings = new List<string>()
        {
            "value0", "value1", "value2"
        };

        [HttpGet]
        public List<string> GetValues()
        {
            return strings;
        }

        [HttpPost("{input}")]
        public List<string> Post(string input)
        {
            strings.Add(input);
            return strings;
        }
    }
}

When I run the code using IIS explorer, and then navigate to https://localhost:44312/GeneratorStatus/, it displays my [HttpGet] correctly (displays the three strings), but when I try to use the post request with, https://localhost:44312/GeneratorStatus/2, it gives me error 405 and doesn't return the string

slam505
  • 13
  • 8

2 Answers2

0

If you are just changing the URL in chrome this would be the problem. 405 often will mean that you are using the wrong http request type. When just changing the URL browsers issue a GET request to that resource.

You may want to test that same POST method with Postman.

Also see: Asp.Net Core 3.1 405 Method Not Allowed

Nolan Bradshaw
  • 464
  • 6
  • 14
0

First thing first.

I could be wrong here, but https://localhost:44312/GeneratorStatus/2, is something I would only use when I am getting things. In my experience, I have never seen a POST URL that looks like that.

Second,

I think, you are doing the Post wrong. First Up, I hope you are using Postman or curl to test your endpoints.

your POST would be something like this.

URL - If I am POSTing, the URL would be

https://localhost:44312/GeneratorStatus

with the Post Body, in your case, is a simple string, would look something like this.

{
  input : "some input value"
}

Your Controller should probably look like this.

    [HttpPost]
    public List<string> Post(string input)
    {
        strings.Add(input);
        return strings;
    }
Jay
  • 2,648
  • 4
  • 29
  • 58