C#C
C#2y ago
Mastalt

MVC App's Post request not reaching

First off, yes I already asked this last friday but couldn't follow trough due to not having the source code for the weekend.

Either way, here's my problem: I have an MVC Web Api in .NET 8.0, all Get requests from all controllers work as intended, but for Post requests on the other hand, they all return an HTTP Error 405 (Method not Allowed). Here is my initialization class and one of my Post method (they are all basiaclly structured the same) (Also, all the Cors stuff are things I already tried with no avail)

Initialization Class:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<SkyblockRessourcesAPIContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("SkyblockRessourcesAPIContext") ?? throw new InvalidOperationException("Connection string 'SkyblockRessourcesAPIContext' not found."), x => x.UseNetTopologySuite()));

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddCors(o => o.AddDefaultPolicy(builder =>
{
    builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
}));

var app = builder.Build();

app.UseCors(builder =>
{
    builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();


Controller Example:
[Route("api/[controller]")]
[ApiController]
public class RegionsController : ControllerBase
{
    private readonly SkyblockRessourcesAPIContext _context;

    public RegionsController(SkyblockRessourcesAPIContext context)
    {
        _context = context;
    }

    // GET: api/Regions/Get
    [HttpGet]
    [Route("Get")]
    public async Task<ActionResult<IEnumerable<Region>>> GetRegion()
    {
        return await _context.Region.ToListAsync();
    }

    // POST: api/Regions/Post
    [EnableCors("*", "*", "*")]
    [HttpPost]
    [Route("Post")]
    public async Task<ActionResult<Region>> PostRegion([FromBody] Region region)
    {
        _context.Region.Add(region);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetRegion", new { id = region.Id }, region);
    }

    // DELETE: api/Regions/Delete
    [HttpDelete]
    [Route("Delete/all")]
    public async Task<IActionResult> DeleteAllRegion()
    {
        var region = await _context.Region.ToListAsync();
        if (region == null)
        {
            return NotFound();
        }

        _context.Region.RemoveRange(region);

        return NoContent();
    }

    // DELETE: api/Regions/Delete?id=5
    [HttpDelete]
    [Route("Delete")]
    public async Task<IActionResult> DeleteRegion(int id)
    {
        var region = await _context.Region.FindAsync(id);
        if (region == null)
        {
            return NotFound();
        }

        _context.Region.Remove(region);
        await _context.SaveChangesAsync();

        return NoContent();
    }
Was this page helpful?