删除webapi的删除操作方法没有被触发。 [英] Delete action method of webapi is not getting triggered.

查看:183
本文介绍了删除webapi的删除操作方法没有被触发。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVC项目中有一个web api控制器,如下所示



I have a web api controller in my MVC project as follows

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 Delete(int id)
     {
         Employee emp = repository.Get(id);
         if (emp == null)
         {
             throw new HttpResponseException(HttpStatusCode.NotFound);
         }
         repository.Remove(id);
     }
}





对我来说,添加(Put),检索(获取)和更新(发布)方法被触发



但是,删除方法没有被触发



我的删除方法的URL是:



http:// localhost:63187 / api / Employee / 2



和我的Webapicongif。 cs





For me, Add(Put), Retrieve (get) and Update (Post) method getting triggered

But, Delete method is not getting triggered

And my URL of Delete Method is :

http://localhost:63187/api/Employee/2

and my 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文件如下




and the global.cs file as follows

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

    }





我的尝试:



当我执行删除操作时,



i收到错误报告



消息:找不到与请求URI匹配的HTTP资源'http:// localhost:63187 / api / Employee / 2'。



MessageDetail



控制器'员工'上没有找到与名称'2'匹配的操作。

名称



一般:



请求网址:http:// localhost:63187 / api / Employee / 2

申请方法:DELETE

状态代码:404 Not Found

远程地址:[:: 1]:63187



响应标题



访问控制 - 允许 - 方法:GET,POST,PUT ,删除,选项

缓存控制:无缓存

内容长度:204

内容类型:application / json; charset = utf-8

日期:2017年3月30日星期四12:00:21 GMT

E xpires:-1

Pragma:no-cache

服务器:Microsoft-IIS / 10.0

X-AspNet-版本:4.0.30319

X-Powered-By:ASP.NET

X-SourceFiles:=?UTF-8?B?RDpcU2FtcGxlXE1WQ1xEYXRhTWFuYWdlclxBZGFwdG9yc1xTYW1wbGVcYXBpXEVtcGxveWVlXDI =?=



请求标题



接受:* / *

接受编码:gzip, deflate,sdch,br

接受语言:en-US,en; q = 0.8

连接:keep-alive

内容长度:1

内容类型:application / json; charset = UTF-8

DataServiceVersion:2.0

主持人:localhost:63187

MaxDataServiceVersion:2.0
来源: http:// localhost:63187

Referer:http:// localhost:63187 / CRUDRemote / Remove

User-Agent:Mozilla / 5.0(Windows NT 6.3; Win64; x64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 56.0.2924.87 Safari / 537.36

X-Requested-With:XMLHttpRequest



请求有效负载



2



我犯了错误。



What I have tried:

When i do the delete operation,

i got an error report

Message : "No HTTP resource was found that matches the request URI 'http://localhost:63187/api/Employee/2'.

MessageDetail
:
"No action was found on the controller 'Employee' that matches the name '2'."
Name

General:

Request URL:http://localhost:63187/api/Employee/2
Request Method:DELETE
Status Code:404 Not Found
Remote Address:[::1]:63187

Response Header

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=?=

Request Header

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

Request Payload

2

Where i am committing the mistake.

推荐答案

最后我发现了问题,我在Global.cs和WebApiConfig中犯了错误



已更新的Global.cs已提交



At last i found the issue, i have done the mistake in Global.cs and WebApiConfig

Updated Global.cs filed

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

        //GlobalConfiguration.Configure(WebApiConfig.Register);

        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);


    //    WebApiConfig.Register(GlobalConfiguration.Configuration);
    }







和WebApiConfig






and WebApiConfig

public static void Register(HttpConfiguration config)
    {
        //config.MapHttpAttributeRoutes();

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



现在,已触发删除操作更新。


Now, the Delete action updated triggered.


这篇关于删除webapi的删除操作方法没有被触发。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆