locked
405 method not allowed. message : the requested resource does not support http method 'DELETE'. RRS feed

  • Question

  • User847472716 posted

    Not able to get the delete method to work. 

    Delete method in controller :

    [HttpDelete]
    public HttpResponseMessage Delete([FromUri]int id)
    {
    try
    {
    using (InventorySystemEntities invEntity = new InventorySystemEntities())
    {
    var entity = invEntity.Assets.FirstOrDefault(e => e.AssetID == id);
    if (entity == null)
    {
    return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Asset with Id = " + id.ToString() + " not found");
    }
    else
    {
    invEntity.Assets.Remove(entity);
    invEntity.SaveChanges();
    return Request.CreateResponse(HttpStatusCode.OK);
    }
    }
    }
    catch(Exception ex)
    {
    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
    }
    }

    Web.config:

    <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule" />
    <remove name="TelemetryCorrelationHttpModule" />
    <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
    <remove name="ApplicationInsightsWebTracking" />
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
    <remove name="WebDAV" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    </system.webServer>

    WebApi.config :

    // Web API configuration and services
    var corsAttr = new EnableCorsAttribute("http://localhost:49803/", "*", "*");
    config.EnableCors(corsAttr);

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );

    Wednesday, November 21, 2018 8:42 PM

All replies

  • User1120430333 posted

    A HttpPost attribute should work.

    Thursday, November 22, 2018 12:39 AM
  • User-271186128 posted

    Hi cpilla095,

    Please try to remove the FromUri attribute.

    after modified, code as below:

            [HttpDelete]
            public HttpResponseMessage Delete(int id)
            {

    Best regards,
    Dillion

    Thursday, November 22, 2018 2:30 AM
  • User-474980206 posted

    try adding to web.config:

    system.webServer>
     <security>
        <requestFiltering>
            <verbs allowUnlisted="false">
                <add verb="GET" allowed="true" />
                <add verb="POST" allowed="true" />
                <add verb="DELETE" allowed="true" />
                <add verb="PUT" allowed="true" />
            </verbs>
        </requestFiltering>
    </security>
    

    Thursday, November 22, 2018 8:04 PM