[英]How do I get WebAPI to accept a complex object as its argument for a DELETE HTTP verb without using C# attributes?
[英]How to configure c# WebApi HTTP DELETE response
我有一个WebApi应用程序的api控制器,该应用程序可以返回GET请求,但不能返回DELETE或PUT。 控制器是:
// DELETE: api/Employees/5
[HttpDelete]
[EnableCors(origins: "https://chad-test4.clas.uconn.edu", headers: "*", methods: "*")]
public HttpResponseMessage Deleteemployee(int id)
{
employee employee = db.employees.Find(id);
var jsonid = employee.id;
if (jsonid == null)
{
var EmpNull = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error: Employee does not exist.");
EmpNull.Content = new StringContent("{ Error: Employee Null }", Encoding.UTF8, "application/json");
return EmpNull;
}
db.employees.Remove(employee);
db.SaveChanges();
var Success = this.Request.CreateResponse(HttpStatusCode.OK, "Success");
Success.Content = new StringContent("{ Success: Success }", Encoding.UTF8, "application/json");
return Success;
}
对于来自其他服务器的网页的以下请求。
<script>
$(function () {
$("#Save").click(function () {
$.ajax({
type: "Delete",
crossDomain: true,
withCredentials: true,
url: "https://chad-test4.clas.uconn.edu/api/Employees/1",
dataType: "json",
success: function (data) {
console.log("Response recieved");
console.log("Success: " + JSON.stringify(data));
},
error: function() {
console.log("Failed"+Error.toString())
}
// data: {"id": 1 }
});
});
});
</script>
我得到以下回应:
XMLHttpRequest cannot load https://uconn.edu/api/Employees/1. Invalid HTTP
status code 405
create.htm:43 Failedfunction Error() { [native code] }
我试图在WebApiConfig中路由此路由,但我相信问题出在这里,我只是不确定如何在此处路由此DELETE操作:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.EnableCors();
var cors = new EnableCorsAttribute("https://chad-dev.clas.uconn.edu", "*", "*");
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DeleteApi",
routeTemplate: "api/Employees/{id}",
defaults: new { controller = "Employees", action = "Deleteemployee", id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
}
}
您确定IIS已配置为处理PUT和DELETE动词吗? 状态405表示不允许使用该方法,请点击此处的链接添加支持。
希望能帮助到你。
也许您已经更改了:
config.EnableCors();
var cors = new EnableCorsAttribute("https://chad-dev.clas.uconn.edu", "*", "*");
对此:
var cors = new EnableCorsAttribute("https://chad-dev.clas.uconn.edu", "*", "*");
config.EnableCors(cors);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.