1

我正在用MVC4 WebApi创建一个jqGrid。我能够将数据填充到网格中,但在单击删除按钮时出现错误。这是我的前端(htm)代码。 :jqGrid - 删除操作Error-“error Status:'Method Not Allowed'。错误代码:405”

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta charset="utf-8"/> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/> 
    <title>My First Grid</title> 
    <link href="../Content/Site.css" rel="stylesheet"/> 
    <!--<link href="../Content/themes/base/jquery.ui.all.css" rel="stylesheet"/>--> 
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css"/> 
    <!--<link href="../Content/ui.jqgrid.css" rel="stylesheet"/>--> 
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.9.2/css/ui.jqgrid.css"/> 
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"/> 
    <style> 
     html, body { 
      font-size: 75%; 
     } 
    </style> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <!--<script src="../Scripts/jquery-1.9.1.min.js"></script>--> 
    <!--<script src="../Scripts/jquery-ui-1.10.4.js"></script>--> 
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
    <script src="../Scripts/free-jqGrid/jquery.jqgrid.src.js"></script> 

    <script> 
     //<![CDATA[ 
     $(document).ready(function() { 
      "use strict"; 
      var apiUrl = "/WebApiOne/api/task/"; 
      jQuery("#gridMain").jqGrid({ 
       url: apiUrl, 
       editurl: apiUrl, 
       datatype: "json", 
       gridview: true, 
       height: "auto", 
       iconSet: "fontAwesome", 
       autoencode: true, 
       sortable: true, 
       viewrecords: true, 
       loadonce: true, 
       jsonReader: { id: "TaskID" }, 
       prmNames: { id: "TaskID" }, 
       colNames: ["TaskID", "ProjectID", "ProjectName", "TaskName", "TaskStatus"], 
       colModel: [ 
        { name: "TaskID", width: 60, key: true, editable: false, sorttype: "int" }, 
        { name: "ProjectID", width: 90 }, 
        { name: "ProjectName", width: 190 }, 
        { name: "TaskName", width: 170, align: "right" }, 
        { name: "TaskStatus", width: 170, align: "right" } 
       ], 
       cmTemplate: { editable: true }, 
       //autowidth: true, 
       formEditing: { 
        width: 400, 
        reloadGridOptions: { fromServer: true }, 
        serializeEditData: function (postdata) { 
         var copyOfPostdata = $.extend({}, postdata); 
         if (postdata.TaskID === "_empty") { // ADD operation 
          postdata.TaskID = 0; // to be easy to deserialize 
         } 
         delete copyOfPostdata.oper; // remove unneeded oper parameter 
         return copyOfPostdata; 
        } 
       }, 
       formDeleting: { 
        mtype: "DELETE", 
        reloadGridOptions: { fromServer: true }, 

        serializeDelData: function() { 
         return ""; // don't send any body for the HTTP DELETE 
        }, 
        onclickSubmit: function (options, postdata) { 
         var p = $(this).jqGrid("getGridParam"); // get reference to internal parameters 
         p.datatype = "json"; 
         options.url = apiUrl + encodeURIComponent(postdata[0]); 
        } 
       }, 
       pager: true 
      }).jqGrid("navGrid", {addtext:"add" , deltext:"del", edittext:"edit"}, { 
       mtype: "PUT", 
       onclickSubmit: function (options, postdata) { 
        //var p = $(this).jqGrid("getGridParam"); // get reference to internal parameters 
        //p.datatype = "json"; // reset datatype to reload from the server 
        options.url = apiUrl + encodeURIComponent(postdata[this.id + "_id"]); 
       } 
      }).jqGrid("filterToolbar") // add searching toolbar for local sorting (bacsue of loadonce:true) in the grid 
       .jqGrid("gridResize"); 
     }); 
     //]]> 
    </script> 
</head> 
<body> 
    <table id="gridMain"></table> 
</body> 
</html> 

我研究了这个很多,做很多变化,但结果总是相同:(试过solution这里提供,但仍然没有成功我一直在我的服务器端代码breakpointGetDelete方法。只有Get方法到达断点时,页面加载Delete方法断点从未打这里去我的服务器端代码:。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Web.Mvc; 

namespace WebApiOne.Controllers 
{ 
    public class Task 
    { 
     public int TaskID { get; set; } 
     public int ProjectID { get; set; } 
     public string ProjectName { get; set; } 
     public string TaskName { get; set; } 
     public string TaskStatus { get; set; } 
    } 

    public class TaskController : ApiController 
    { 
     // GET api/task 
     public IEnumerable<Task> Get() 
     { 
      Task[] tasks = new Task[2]; 

      tasks[0] = new Task() 
      { 
       TaskID = 1, 
       ProjectID = 1, 
       ProjectName = "ProjectOne", 
       TaskName = "FirstPage Development", 
       TaskStatus = "InProgress" 

      }; 

      tasks[1] = new Task() 
      { 
       TaskID = 2, 
       ProjectID = 1, 
       ProjectName = "ProjectOne", 
       TaskName = "Second Page Development", 
       TaskStatus = "Yet To Start" 

      }; 

      return tasks; 
     } 

     // DELETE api/task/5 
     public void Delete(int id) 
     { 
      // Delete row in DB. 
     } 

    } 
} 

这里是删除请求的提琴手跟踪:

DELETE http://localhost/WebApiOne/api/task/2 HTTP/1.1 
Host: localhost 
Connection: keep-alive 
Accept: */* 
Origin: http://localhost 
X-Requested-With: XMLHttpRequest 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36 
Referer: http://localhost/WebApiOne/Views/JqGrid.htm 
Accept-Encoding: gzip, deflate, sdch 
Accept-Language: en-US,en;q=0.8 

Here是错误的屏幕截图:

所有的建议被邀请:)。

回答

2

我无法在计算机上重现问题。以任何方式,我确信你可以在服务器端配置的问题。例如the article描述了密切的问题。可能您与IIS中的WebDAV有冲突。文章建议加入<handlers><system.webServer><remove name="WebDAV" />。例如,你可以修改的web.config

<system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <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> 
</system.webServer> 

以下<system.webServer>节以下

<system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <handlers> 
     <remove name="WebDAV" /> <!-- The Modification !!! --> 
     <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> 
</system.webServer> 

此外,您可以替换线

<modules runAllManagedModulesForAllRequests="true" /> 
web.config<system.webServer>

<modules runAllManagedModulesForAllRequests="true"> 
    <remove name="WebDAVModule"/> <!-- add this --> 
</modules> 

参见the answer

更新:如果以上没有任何帮助,那么我会建议您只卸载WebDAV模块,以验证我们的问题是否真的是WebDAV。如果你不需要WebDAV,我会建议卸载模块。如果您可能需要WebDAV,那么我会建议您在https://serverfault.com/上发布问题。如果卸载WebDAV解决了以下问题,那么您可以确保只有IIS的相应配置以及WebDAV模块的使用方面存在问题,而WebDAV模块的使用不需要Web站点的工作。

+0

我已经从IIS7中删除了该网站并安装在IIS Express上,问题得到了解决。为什么这样的行为我已经尝试了在回答中提到的更改,但这些也没有为IIS7工作。 IIS Express用于默认的'web.config'。 –

+0

为什么它没有为IIS7工作或工作的任何原因? –

+0

@SandeepKushwah:回答你的问题是不同的,因为这是IIS配置的全部问题。你在IIS上安装WebDAV吗?你需要它吗?你在网站上使用启用的“Windows身份验证”吗?您可以禁用该站点的IIS配置中的WebDAV(单击“禁用WebDAV”,请参阅[此处](http://www.iis.net/learn/install/installing-publishing-technologies/installing-and-configuring-webdav-上IIS))。你使用哪种操作系统? – Oleg

相关问题