3

I tried to create a simple HTML form like this using ASP.NET Core tag helpers:

 <form asp-action="RemoveScheduledTenancyDeletion" method="put" asp-route-tenancyId="@Model.Tenancy.TenancyId">          
      <div class="form-group">              
           <input type="submit" value="Remove sheduled tenancy deletion" class="btn btn-default" />
      </div>
 </form>

In the TenancyController there is a simple action:

    [HttpPut]
    public async Task<IActionResult> RemoveScheduledTenancyDeletion(Guid tenancyId)
    {
          //some logic here
         return RedirectToAction(nameof(List));
    }

I tried to check via developer tools if the form was generated properly: enter image description here

and it seems ok to me, however, when I try to send a form Im getting an error: enter image description here

It seems that somehow the whole request is transformed into the HttpGet action. Any ideas what can be wrong?

Out of the scope, I have a separate REST API where such kind of action is defined as HttpPut and it works ok with Angular and other client-side apps which communicate with the API so I don't see any reason why I should not use HttpPut as well in the ASP.NET Core MVC app. Am I missing something? Cheers

GoldenAge
  • 2,918
  • 5
  • 25
  • 63

2 Answers2

7

HTML forms don't support PUT, DELETE, etc.: only GET and POST. If you need to send a PUT, you'll need to use AJAX to do so.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I work with Visual Studio. I think it would be nice to add a feature which will automatically detect the use of `method="put"` on the HTML `form` and notify the developer that it cannot be used e.g. when trying to compile a project – GoldenAge Aug 23 '19 at 13:22
  • I think it's probably intentionally done that way. It's no so much an HTML spec thing, as just simply no browser supports it. That could conceivably change at some point in the future (though I doubt it). Intellisense is only going to go by the spec, though, not how browsers are choosing to implement said spec. – Chris Pratt Aug 23 '19 at 13:25
0

Try adding this decorator to your Controller action

[ValidateAntiForgeryToken]

To me, looks like your view is sending the Anti-forgery token, but your action is not expecting it.

Mauricio Atanache
  • 2,424
  • 1
  • 13
  • 18
  • 1
    ASP.NET Core applies `ValidationAntiForgeryTokenAttribute` globally by default. This has nothing to do with anything here, anyways. PUT is not a supported verb for HTML forms. – Chris Pratt Aug 23 '19 at 12:57