I'm getting a 405 error when trying to do a simple post with a delete. I can read, write, and update just fine, but cannot figure out why Visual Studio 2017 (or, maybe more specifically, IIS Express) won't let me perform a delete.
The internet has a lot of posts regarding web.config
, but they're older posts and the more recent one's don't work. In fact, there was no web.config
included in my project because Microsoft is moving towards appsettings.json
, I believe.
I'm running Visual Studio 2017 v15.9.11 with .NET Core 2.2 in a Docker container. I switched to IIS Express with no luck. I've looked at the applicationhost.config
file for IIS Express and WebDav is commented out already. I believe it's happening at the web server level because I'm not getting a hit on the controller when debugging. My best guess is there's a default setting somewhere that's not allowing deletes; I just can't find it.
namespace Efficacy.Models.Entities
{
/* dbo.Contact.Contact */
public class Contact
{
private const string V = "First Name";
public int Id { get; set; }
public ContactType ContactType { get; set; }
public int ContactTypeId { get; set; }
[Required]
//[Display( Description = ("Delete")]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required]
public string LastName { get; set; }
}
}
namespace Efficacy.Data
{
public class EfficacyDBContext : DbContext
{
public virtual DbSet<Contact> Contact { get; protected set; }
public virtual DbSet<ContactType> ContactType { get; protected set; }
public virtual DbSet<HelloNurse> HelloNurse { get; protected set; }
public EfficacyDBContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ContactEntityTypeConfiguration());
modelBuilder.ApplyConfiguration(new ContactTypeEntityTypeConfiguration());
}
}
}
namespace Efficacy.Areas.Ops.Controllers
{
[Area("Ops")]
public class ContactController : Controller
{
private readonly EfficacyDBContext _db;
public ContactController(EfficacyDBContext db)
{
_db = db;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ContactDelete(int id)
{
var contact = await _db.Contact.FindAsync(id);
if (contact == null)
{
return NotFound();
}
_db.Contact.Remove(contact);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
…
@model Efficacy.Models.Entities.Contact
@{
ViewData["Title"] = "Add Contact";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container-fluid">
<div class="row">
<div class="col-6">
<h2 class="text-info">Add Contact</h2>
</div>
<div class="col-6 text-right">
 
</div>
</div>
<div>
<form method="post" asp-area="Ops" asp-controller="Contact"
asp-action="ContactDelete">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<table class="table table-striped border shadow p-3 mb-5 bg-white rounded">
<tr class="table-secondary">
<th><label asp-for="FirstName">First Name</label></th>
<th><label asp-for="MiddleName">Middle</label></th>
<th><label asp-for="LastName">Last Name</label></th>
<th><label asp-for="ContactTypeId">Contact Type</label></th>
<th class="text-right"> </th>
</tr>
<tr>
<td>
<input asp-for="FirstName" class="form-control"
value="@Model.FirstName" />
<span asp-validation-for="FirstName"
class="text-danger"></span>
</td>
<td>
<input asp-for="MiddleName" class="form-control"
value="@Model.MiddleName" />
<span asp-validation-for="FirstName"
class="text-danger"></span>
</td>
<td>
<input asp-for="LastName" class="form-control"
value="@Model.LastName" />
<span asp-validation-for="FirstName"
class="text-danger"></span>
</td>
<td>
@* TODO: Bind *@
<select name="ContactTypeId" class="form-control">
<option value="1">Organization</option>
<option value="2">Lead</option>
<option value="3">Event</option>
<option value="4">Invoice</option>
<option value="5">Contact</option>
</select>
</td>
<td class="text-right">
<input type="submit" class="btn btn-info form-control mb-1"
asp-route-id="@Model.Id" value="Save" />
<a asp-area="ops" asp-controller="Contact"
asp-action="ContactDelete" asp-route-id="@Model.Id"
class="btn btn-danger form-control mb-1">Delete</a>
<a asp-action="Index"
class="btn btn-success form-control">Cancel</a>
</td>
</tr>
</table>
</form>
</div>
</div>
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET https://localhost:44382/Ops/Contact/ContactDelete/11
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET https://localhost:44382/Ops/Contact/ContactDelete/11
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executing endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executed endpoint '405 HTTP Method Not Supported'
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint '405 HTTP Method Not Supported'
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 14.0081ms 405
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area=Ops}/{controller=Home}/{action=Index}/{id?}");
});
405
HTTP code means Method Not Allowed
. Served returned this error because action expects POST
request (since it's marked with [HttpPost]
) and when user click on anchor tag ( <a>
) browser sends GET
request. In order to send POST
you need to use form
tag
<form method="post" asp-action="ContactDelete" asp-area="Ops" asp-controller="Contact">
<input name="Id" value="@item.Id" type="hidden" /> <!-- hidden input contains Id value -->
<button type="submit">Delete</button>
</form>
Also you have [ValidateAntiForgeryToken]
attribute on the action so it needs anti-forgery token to be sent. FormTagHelper will automatically generate hidden input
with this token for you so you don't need to do anything else.
i believe your issue is routing problem can you try this.
[HttpPost("id")] <----------
[ValidateAntiForgeryToken]
public async Task<IActionResult> ContactDelete([FromRoute(Name = "id" )] int id) <----------
{
var contact = await _db.Contact.FindAsync(id);
if (contact == null)
{
return NotFound();
}
_db.Contact.Remove(contact);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
When you use POST method,it's better to include "id" in hidden element in form scope and send it with other values:
<input type="hidden" asp-for="@Model.Id" />
I see you are trying to update and delete the same form.then you should use two Submit buttons with different Actions
<button type="submit" asp-action="Save" asp-controller="Contact" asp-area="Ops" class="btn form-control">
<span>Update</span>
</button>
<button type="submit" asp-action="ContactDelete" asp-controller="Contact" asp-area="Ops" class="btn form-control">
<span>Delete</span>
</button>
one more thing... remove Controller,Action and Area tags in form element and let submit buttons handle that:
<form asp-action="" method="post" enctype="multipart/form-data">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.