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 }
);