jqGrid - 删除操作错误 - “错误状态:'方法不允许'。错误代码:405”

时间:2015-09-19 19:52:33

标签: c# asp.net-mvc-4 jqgrid http-status-code-405 methodnotfound

我正在使用MVC4 WebApi创建一个jqGrid。我能够将数据填充到网格中,但在单击删除按钮时出错。这是我的前端(htm)代码。 :

<!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,但仍然没有成功。我在breakpoint上保留Get我的服务器端代码的Delete方法。只有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是错误的屏幕截图:

邀请所有建议:)。

1 个答案:

答案 0 :(得分:2)

我无法在计算机上重现问题。无论如何,我确信您可以解决服务器端配置的问题。例如,the article描述了密切问题。可能与IIS上的WebDAV冲突。该文章建议在行<handlers>的{​​{1}} <system.webServer>中添加包含。例如,您可以修改<remove name="WebDAV" />

的以下<system.webServer>部分
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>
    <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" />

<system.webServer>部分的

web.config

请参阅the answer

更新:如果上面的任何内容都无法帮助您,那么我建议您只需卸载WebDAV模块,以验证我们问题的原因是否真的是WebDAV。如果您不需要WebDAV,我建议您重新安装模块。如果您需要WebDAV,我建议您在https://serverfault.com/上发布问题。如果卸载WebDAV解决了以下问题:您可以确定只有IIS的相应配置存在问题,并且使用了WebDAV模块,而您不需要使用该网站。