Delete route not being hit

2019-09-05 18:16发布

问题:

Being battling this issue for too long now it's time to ask for help. The delete route of my .net core mvc app is not being hit. All other routes (Get, Post) are hit just fine. The route config at startup looks like this:

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller}/{action}/{id?}");
    });

The form submit looks something like this:

    <a class="btn btn-primary" href="~/DisabledRegistrations/Delete/@(Model.Id)">Confirm</a>

The controller method looks like this:

[Authorize]
[HttpDelete]
public async Task<IActionResult> Delete(string id)
{
  ...
}

But hitting that with something like:

https://localhost:8307/DisabledRegistrations/Delete/f17dff6b3fcd43ba89eab4bbad5c992e

results in:

No webpage was found for the web address: 
https://localhost:8307/DisabledRegistrations/Delete/f17dff6b3fcd43ba89eab4bbad5c992e

We run in Service Fabric and not sure if there's any particulars to that. Our web.config has this although not sure if it's relevant in that context:

  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>

Any hints for things to try are greatly appreciated.

UPDATE:

If I actually do submit a form as follows:

<form asp-controller="DisabledRegistrations" asp-action="Delete" method="delete" asp-route-id="@Model.Id">
    <div class="text-right">
        <button type="submit" class="btn btn-primary">Confirm</button>
        <a class="btn btn-primary" href="~/DisabledRegistrations/Index">Cancel</a>
    </div>
</form>

Then I still get:

No webpage was found for the web address: 
https://localhost:8307/DisabledRegistrations/Delete/f17dff6b3fcd43ba89eab4bbad5c992e?__RequestVerificationToken=CfDJ8KwmDf2MXHlGrC8zIIntu4IV_83R9jSBwPqk3w8Ymq2VoBnQHN8lxEOHqMUfJwtxX-HLZwr6AWw8uKiVaSz7l-CZjPzZ_IxJhRh31MYiwbgsJzLcQMvrHWGd_sueZ8OLKbRAoYGeVHLfVfkjac-TCaLE9CoOYSCyhY4EDtrFhiLVY3_3h-bJTSLYTT2E7qXcvA

回答1:

[HttpDelete]

is a verb like post, you are not doing http delete from your link that is just doing http get therefore it doesn't match your route. to do delete verb you would have to use ajax and specify the verb.

you are not submitting a form, you only show an a element that links therefore get not delete verb



回答2:

I am not entirely sure why you are using a form to do this. The < a> tag will do this for you very easily.

<div class="text-right">
    <a class="btn btn-primary" asp-controller="DisabledRegistrations" asp-action="Delete" asp-route-id="@Model.Id">Confirm</a>
    <a class="btn btn-primary" href="~/DisabledRegistrations/Index">Cancel</a>
</div>