C# POST、PUT和DELETE方法赢得';在我的web应用程序中无法工作

C# POST、PUT和DELETE方法赢得';在我的web应用程序中无法工作,c#,angularjs,rest,asp.net-web-api,C#,Angularjs,Rest,Asp.net Web Api,我正在用angularJS和web Api构建小型web应用程序。我已经创建了后端,当我和邮递员一起测试时,一切都很好。但现在,我只是不能让它从我的前端工作,至少不是为邮政;放置和删除方法。 我什么都试过了,不,我有点绝望 这是我的控制器 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http;

我正在用angularJS和web Api构建小型web应用程序。我已经创建了后端,当我和邮递员一起测试时,一切都很好。但现在,我只是不能让它从我的前端工作,至少不是为邮政;放置和删除方法。 我什么都试过了,不,我有点绝望

这是我的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using PlaylistWebApi.Models;
using PlaylistWebApi.Services;

namespace PlaylistWebApi.Controllers
{
    public class SongsController : ApiController
    {

        private readonly ISongService _service;

        public SongsController(ISongService service)
        {
            _service = service;
        }

        public IEnumerable<Song> Get()
        {
            IEnumerable<Song> items = null;
            try
            {
                items = _service.GetAll();
            }
            catch (Exception)
            {
                var message = string.Format("Error while retrieving songs!");
                throw new HttpResponseException(
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
            }

            return items;

        }

        public Song Get(int id)
        {
            Song item = null;
            try
            {
                item = _service.Get(id);
            }
            catch (Exception)
            {
                var message = string.Format("Error while retrieving the song!");
                throw new HttpResponseException(
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
            }

            if (item == null)
            {
                var message = string.Format("Error while retriving song!");
                throw new HttpResponseException(
                    Request.CreateErrorResponse(HttpStatusCode.NotFound, message));
            }
            else
            {
                return item;
            }
        }

        [AllowAnonymous]
        [HttpOptions,HttpPost]
        public Song Post([FromBody]Song song)
        {
            Song item = null;
            try
            {
                item = _service.Add(song);
            }
            catch (Exception)
            {
                var message = string.Format("Error while adding new song!");
                throw new HttpResponseException(
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
            }
            return item;
        }

        [AllowAnonymous]
        [HttpOptions,HttpPut]
        public void Put(int id, [FromBody]Song item)
        {
            try
            {
                _service.Update(item);
            }
            catch (Exception)
            {
                var message = string.Format("Error while editing song!");
                throw new HttpResponseException(
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
            }
        }

        [AllowAnonymous]
        [HttpOptions,HttpDelete]
        public void Delete(int id)
        {
            try
            {
                _service.Remove(id);
            }
            catch (Exception)
            {
                var message = string.Format("Error while removing song!");
                throw new HttpResponseException(
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, message));
            }
        }
    }
}
删除方法出错

Remote Address:[::1]:9000
Request URL:http: //localhost:9000/api/songs/5
Request Method:OPTIONS
Status Code:500 Internal Server Error
Response Headers
view source
Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:*
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:2236
Content-Type:application/json; charset=utf-8
Date:Fri, 03 Jul 2015 00:22:25 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpccmVwb3NpdG9yeVxQbGF5bGlzdFdlYkFwaVxQbGF5bGlzdFdlYkFwaVxhcGlcc29uZ3NcNQ==?=
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept
Access-Control-Request-Method:DELETE
Connection:keep-alive
Host:localhost:9000
Origin:http: //127.0.0.1:50119
Referer:http: //127.0.0.1:50119/index.html



Operation=ApiControllerActionSelector.SelectAction, Exception=System.InvalidOperationException: Multiple actions were found that match the request: 
Put on type PlaylistWebApi.Controllers.SongsController
delete on type PlaylistWebApi.Controllers.SongsController
   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.<>c__DisplayClass2.<System.Web.Http.Controllers.IHttpActionSelector.SelectAction>b__0()
   at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)
iisexpress.exe Error: 0 : Operation=SongsController.ExecuteAsync, Exception=System.InvalidOperationException: Multiple actions were found that match the request: 
Put on type PlaylistWebApi.Controllers.SongsController
delete on type PlaylistWebApi.Controllers.SongsController
   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.<>c__DisplayClass2.<System.Web.Http.Controllers.IHttpActionSelector.SelectAction>b__0()
   at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)
   at System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.System.Web.Http.Controllers.IHttpActionSelector.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
   at System.Web.Http.Tracing.Tracers.HttpControllerTracer.<ExecuteAsyncCore>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext()
iisexpress.exe Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance
iisexpress.exe Information: 0 : Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
iisexpress.exe Information: 0 : Response, Status=500 (InternalServerError), Method=OPTIONS, Url=http: //localhost:9000/api/songs/5, Message='Content-type='application/json; charset=utf-8', content-length=unknown'
iisexpress.exe Information: 0 : Operation=JsonMediaTypeFormatter.WriteToStreamAsync
iisexpress.exe Information: 0 : Operation=SongsController.Dispose
远程地址:[::1]:9000
请求URL:http://localhost:9000/api/songs/5
请求方法:选项
状态代码:500内部服务器错误
响应头
视图源
访问控制允许标头:*
访问控制允许方法:*
访问控制允许来源:*
缓存控制:没有缓存
内容长度:2236
内容类型:application/json;字符集=utf-8
日期:2015年7月3日星期五00:22:25 GMT
过期:-1
Pragma:没有缓存
服务器:Microsoft IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDPCCMVWB3NPDG9YEVXQBGF5BGLZDFDLYKFWAXQBGF5BGLZDFDLYKFWAXCGLCC29UZ3NCNQ==
请求头
视图源
接受:*/*
接受编码:gzip、deflate、sdch
接受语言:en-US,en;q=0.8
访问控制请求头:接受
访问控制请求方法:删除
连接:保持活力
主机:localhost:9000
来源:http://127.0.0.1:50119
参考:http://127.0.0.1:50119/index.html
Operation=ApiControllerActionSelector.SelectAction,Exception=System.InvalidOperationException:找到多个与请求匹配的操作:
播放类型:webapi.Controllers.SongsController
删除播放类型webapi.Controllers.SongsController
在System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
在System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
在System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.c_uuDisplayClass2.b_uuu0()中
在System.Web.Http.Tracing.ITraceWriterExtensions.TraceEnginend(ITraceWriter traceWriter、HttpRequestMessage请求、字符串类别、TraceLevel、字符串运算符名称、字符串运算符名称、操作'1 BeginCe、操作执行、操作'1 endTrace、操作'1 errorTrace)
iisexpress.exe错误:0:Operation=SongsController.ExecuteAsync,Exception=System.InvalidOperationException:找到多个与请求匹配的操作:
播放类型:webapi.Controllers.SongsController
删除播放类型webapi.Controllers.SongsController
在System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
在System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
在System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.c_uuDisplayClass2.b_uuu0()中
在System.Web.Http.Tracing.ITraceWriterExtensions.TraceEnginend(ITraceWriter traceWriter、HttpRequestMessage请求、字符串类别、TraceLevel、字符串运算符名称、字符串运算符名称、操作'1 BeginCe、操作执行、操作'1 endTrace、操作'1 errorTrace)
位于System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.System.Web.Http.Controllers.IHttpActionSelector.SelectAction(HttpControllerContext controllerContext)
位于System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken CancellationToken)
在System.Web.Http.Tracing.Tracers.HttpControllerTracer.d_u5.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在System.Web.Http.Tracing.itraceWriteExtensions.d_u18`1.MoveNext()中
iisexpress.exe信息:0:Message='将使用相同的'JsonMediaTypeFormatter'格式化程序',操作=JsonMediaTypeFormatter.GetPerRequestFormatterInstance
iisexpress.exe信息:0:Message='Selected formatter='JsonMediaTypeFormatter',content type='application/json;字符集=utf-8'',操作=DefaultContentCongregator.Congregate
iisexpress.exe信息:0:Response,Status=500(InternalServerError),Method=OPTIONS,Url=http://localhost:9000/api/songs/5,Message='Content-type='application/json;字符集=utf-8',内容长度=未知'
iisexpress.exe信息:0:Operation=JsonMediaTypeFormatter.WriteToStreamAsync
iisexpress.exe信息:0:Operation=SongsController.Dispose
对于POST-an-PUT,我没有收到任何错误,它会将我重定向回相同的带有奇怪uri的位置(我正在发布的对象)。我做错了什么

    <div class="row">
        <div class="col-lg-12">
            <h3 class="page-header"><i class="fa fa-plus "></i> Songs</h3>
            <ol class="breadcrumb">
                <li><i class="fa fa-home"></i><a href="#">Home</a></li>
                <li><i class="icon_mic_alt"></i>Songs</li>
                <li><i class="fa fa-plus "></i>Add New</li>
            </ol>
        </div>
    </div>
      <!-- Form validations -->              
    <div class="row">
          <div class="col-lg-12">
              <section class="panel">
                  <header class="panel-heading">
                      Add New Song
                  </header>
                  <div class="panel-body">
                        <form class="form-validate form-horizontal " id="register_form" method="get" action="">
                             <div class="alert alert-block"  >
                                    <div ng-repeat="alert in alerts"  type="{{alert.type}}" close="closeAlert($index)">
                                        <button data-dismiss="alert" class="close close-sm" type="button" >{{alert.msg}}>
                                            <i class="icon-remove"></i>
                                    </button> 
                                </div>
                              </div>
                              <div class="form-group">
                                  <label for="songname" class="control-label col-lg-2">Song name <span class="required">*</span></label>
                                  <div class="col-lg-10">
                                      <input class="form-control" id="songname" name="songname" ng-model="songName" type="text" />
                                  </div>
                              </div>
                              <div class="form-group">
                                  <label for="filename" class="control-label col-lg-2">File Name <span class="required">*</span></label>
                                  <div class="col-lg-10">
                                      <input class="form-control" id="filename" name="filename" ng-model="fileName" type="text" />
                                  </div>
                              </div>
                              <div class="form-group">
                                  <label for="artistname" class="control-label col-lg-2">Username <span class="required">*</span></label>
                                  <div class="col-lg-10">
                                      <input class="form-control" id="artistname" name="artistname" ng-model="artistName" type="text" />
                                  </div>
                              </div>
                              <div class="form-group">
                                  <label for="album" class="control-label col-lg-2">Album</label>
                                  <div class="col-lg-10">
                                      <input class="form-control" id="album" name="album"  ng-model="album" type="text" />
                                  </div>
                             </div>
                              <div class="form-group">
                                  <div class="col-lg-offset-2 col-lg-10">
                                      <button class="btn btn-primary" ng-click="addSong()">Save</button>
                                  </div>
                              </div>
                          </form>
                      </div>
                </section>
            </div>
        </div>

歌曲
  • 歌曲
  • 新增
  • 添加新歌 {{alert.msg}}> 歌名* 文件名* 用户名*
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header"><i class="fa fa-plus "></i> Songs</h3>
                <ol class="breadcrumb">
                    <li><i class="fa fa-home"></i><a href="#">Home</a></li>
                    <li><i class="icon_mic_alt"></i>Songs</li>
                    <li><i class="fa fa-plus "></i>Add New</li>
                </ol>
            </div>
        </div>
          <!-- Form validations -->              
        <div class="row">
              <div class="col-lg-12">
                  <section class="panel">
                      <header class="panel-heading">
                          Add New Song
                      </header>
                      <div class="panel-body">
                            <form class="form-validate form-horizontal " id="register_form" method="get" action="">
                                 <div class="alert alert-block"  >
                                        <div ng-repeat="alert in alerts"  type="{{alert.type}}" close="closeAlert($index)">
                                            <button data-dismiss="alert" class="close close-sm" type="button" >{{alert.msg}}>
                                                <i class="icon-remove"></i>
                                        </button> 
                                    </div>
                                  </div>
                                  <div class="form-group">
                                      <label for="songname" class="control-label col-lg-2">Song name <span class="required">*</span></label>
                                      <div class="col-lg-10">
                                          <input class="form-control" id="songname" name="songname" ng-model="songName" type="text" />
                                      </div>
                                  </div>
                                  <div class="form-group">
                                      <label for="filename" class="control-label col-lg-2">File Name <span class="required">*</span></label>
                                      <div class="col-lg-10">
                                          <input class="form-control" id="filename" name="filename" ng-model="fileName" type="text" />
                                      </div>
                                  </div>
                                  <div class="form-group">
                                      <label for="artistname" class="control-label col-lg-2">Username <span class="required">*</span></label>
                                      <div class="col-lg-10">
                                          <input class="form-control" id="artistname" name="artistname" ng-model="artistName" type="text" />
                                      </div>
                                  </div>
                                  <div class="form-group">
                                      <label for="album" class="control-label col-lg-2">Album</label>
                                      <div class="col-lg-10">
                                          <input class="form-control" id="album" name="album"  ng-model="album" type="text" />
                                      </div>
                                 </div>
                                  <div class="form-group">
                                      <div class="col-lg-offset-2 col-lg-10">
                                          <button class="btn btn-primary" ng-click="addSong()">Save</button>
                                      </div>
                                  </div>
                              </form>
                          </div>
                    </section>
                </div>
            </div>
    
    Multiple actions were found that match the request: 
      Put on type PlaylistWebApi.Controllers.SongsController
      delete on type PlaylistWebApi.Controllers.SongsController
    
    [HttpOptions]
    public HttpResponseMessage Options()
    {
        return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
    }
    
    <form id="register_form" ng-submit="addSong()">
        <button type="submit" class="btn btn-primary">Save</button>
    </form>