【发布时间】:2017-08-24 08:07:16
【问题描述】:
我的项目中有一个web api控制器,如下
public class EmployeeController : ApiController
{
static readonly IEmployeeRepository repository = new EmployeeRepository();
// GET api/<controller>
[HttpGet]
public object Get()
{
var queryString = HttpContext.Current.Request.QueryString;
var data = repository.GetAll().ToList();
return new { Items = data, Count = data.Count() };
}
public Employee GetEmployee(int id)
{
Employee emp = repository.Get(id);
if (emp == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return emp;
}
// POST api/<controller>
public HttpResponseMessage PostEmployee(Employee emp)
{
emp = repository.Add(emp);
var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, emp);
string uri = Url.Link("Employee", new { id = emp.EmployeeID });
response.Headers.Location = new Uri(uri);
return response;
}
[HttpPut]
// PUT api/<controller>
public void PutEmployee(Employee emp)
{
if (!repository.Update(emp))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
[HttpDelete]
public void DeleteEmployee(int id)
{
Employee emp = repository.Get(id);
if (emp == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
repository.Remove(id);
}
}
对我来说,正确触发 get 和 post 方法
但是,删除方法没有被触发。
还有我的 Webapicongif.cs
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Employee",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.EnableQuerySupport();
}
和global.cs文件如下
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
一旦我对员工进行了删除
General:
Request URL:http://localhost:63187/api/Employee/2
Request Method:DELETE
Status Code:404 Not Found
Remote Address:[::1]:63187
响应标头
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Cache-Control:no-cache
Content-Length:204
Content-Type:application/json; charset=utf-8
Date:Thu, 30 Mar 2017 12:00:21 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcU2FtcGxlXE1WQ1xEYXRhTWFuYWdlclxBZGFwdG9yc1xTYW1wbGVcYXBpXEVtcGxveWVlXDI=?=
请求标头
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:1
Content-Type:application/json; charset=UTF-8
DataServiceVersion:2.0
Host:localhost:63187
MaxDataServiceVersion:2.0
Origin:http://localhost:63187
Referer:http://localhost:63187/CRUDRemote/Remove
User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
X-Requested-With:XMLHttpRequest
请求负载
2
我的 ajax 请求如下
beforeSend:function (),
contentType:"application/json; charset=utf-8",
data:"2",
error:function (e),
success:function (),
type:"DELETE",
url:"/api/Employee/2",
我犯错的地方
【问题讨论】:
-
看看这里,看看它是否能解决你的问题same issue maybe
-
从哪里调用 api、MVC 应用程序或 Ajax 请求?
-
可能是托管应用程序的网络服务器不支持该方法?
-
请指定托管您的服务的服务器。 IIS?自托管?
-
嗨,Sulay,我从 Ajax 请求中打来电话
标签: c# asp.net-mvc asp.net-mvc-4 model-view-controller asp.net-web-api