ASP.NET WEBAPI c#在DELETE上给出了404

时间:2013-11-07 21:24:07

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api

我有一个控制器在localhost上可以正常使用所有动词,但是IIS 8上的DELETE失败给404.网络服务器没有安装webdav,绝望我也关掉了FTP :)我只是不知道在哪里看。

我的控制器:

[HttpDelete]
    public HttpResponseMessage Delete()
    {
        oTrace T = new oTrace();


        T.trace("deprovision request");

        string headers = Request.Headers.Authorization.ToString().Split(' ')[1];
        string userpwd = WebConfigurationManager.AppSettings["xxxxuserpwd"];

        //try
        //{
        string basicData = System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(headers));


        if (basicData != userpwd)
        {

            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));
        }
        else
        {


            string[] paths = Request.RequestUri.ToString().Split('/');

            Guid Unique = new Guid(paths[paths.Length - 1]);

            //HttpContent requestContent = Request.Content;
            //string jsonContent = requestContent.ReadAsStringAsync().Result;
            //oProvision myProvision = JsonConvert.DeserializeObject<oProvision>(jsonContent);


            //string JSONresponse = @"{""id"":""1"", ""config"": { ""xxON_UNIQUE"":""" + Convert.ToString(Unique) + @"""} }";
            var response = this.Request.CreateResponse(HttpStatusCode.OK);
            //response.Content = new StringContent(JSONresponse, Encoding.UTF8, "application/json");
            ////kick off GetAppInfoRequest

            BackgroundWorker barInvoker = new BackgroundWorker();
            barInvoker.DoWork += delegate
            {
                Thread.Sleep(TimeSpan.FromSeconds(10));
                deProvisionAddon(Unique);
            };
            barInvoker.RunWorkerAsync();
            return response;

        }

    }

我的web.config

  <system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule" />
</modules>
<handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

我的webapi.config

        public static void Register(HttpConfiguration config)
    {

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "heroku/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        config.Routes.MapHttpRoute(
            name: "DeleteApi",
            routeTemplate: "heroku/{controller}/Delete/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        config.EnableSystemDiagnosticsTracing();
    }
感谢任何帮助,因为我已经和我斗争了好几天。

2 个答案:

答案 0 :(得分:0)

您不需要在routeTemplate中使用“删除”的路线。 WebApi根据您使用的HTTP谓词知道要调用的操作。因此,如果您发出HTTP DELETE请求,您的控制器操作被命名为“删除”,那么它按照惯例就知道要调用它 - 不需要像MVC一样按名称指定它控制器。

答案 1 :(得分:-4)

如果你使用[HttpDelete]定义方法,你应该使用delete call webapi,就像post或get。