No HTTP resource was found that matches the request URI 'http://localhost:xxx:

1.5k views Asked by At

I am working on .net web api.....

Web Api Config :

public static void Register(HttpConfiguration config)
        {
            // Verb Routing 
            RouteTable.Routes.MapHttpRoute(
                 name: "SmallBizApi",
                 routeTemplate: "api/{controller}/{action}/{id}",
                 defaults: new
                 {
                     id = RouteParameter.Optional,
                     action = RouteParameter.Optional
                 }
             );

            config.Formatters.Clear();
            config.Formatters.Insert(0, new SmallBiz.WebAPI.Common.JsonpFormatter());
        }

I am using jsonp format to load data in kendo-ui Gantt chart as can be seen...

<div id="grid"></div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
    $(document).ready(function () {
        var projectdata = "http://localhost:1799/api",
            dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: projectdata + "/project",
                        dataType: "jsonp"
                    },
                    update: {
                        url: projectdata + "/project/put",
                        dataType: "jsonp"
                    },
                    destroy: {
                        url: projectdata + "/project/delete",
                        dataType: "jsonp"
                    },
                    create: {
                        url: projectdata + "/project/post",
                        dataType: "jsonp"
                    },
                    parameterMap: function (options, operation) {
                        if (operation !== "read" && options.models) {
                            return { models: kendo.stringify(options.models) };
                        }
                    }
                },
                batch: true,
                pageSize: 20,
                schema: {
                    model: {
                        id: "ProjectId",
                        fields: {
                            ProjectId: { editable: false, nullable: false },
                            Name: { validation: { required: true } },
                            Status: { validation: { required: true } },
                            IsActive: { type: "boolean" }
                        }
                    }
                }
            });

        $("#grid").kendoGrid({
            dataSource: dataSource,
            pageable: true,
            toolbar: ["create"],
            scrollable: false,
            sortable: true,
            groupable: true,
            columns: [
                { field: "Name", title: "Project Name", width: "170px" },
                { field: "Status", title: "Status", width: "110px" },
                { field: "IsActive", title: "Active", width: "50px" },
                { command: ["edit", "delete", "Setting","Task"], title: "&nbsp;", width: "150px" }
            ],
            editable: "popup"
        });
    });
</script>

Controller code :

public IQueryable<ProjectsDM> GetProject()
        {
            return db.Project;
        }
 [HttpPut]
        public IHttpActionResult PutProjectsDM(int id, ProjectsDM projectsdm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != projectsdm.ProjectId)
            {
                return BadRequest();
            }

            db.Entry(projectsdm).State = EntityState.Modified;

            try
            {
                projectsdm.ModifiedBy = "adnan";
                projectsdm.ModifiedDate = DateTime.Now;
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProjectsDMExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

Get method/action is working fine in kendo grid but i face problem with put method/action(when we try to edit some record in grid)....

The errors in firebug .net tab

Response : "Message": "No HTTP resource was found that matches the request URI 'http://localhost:1799/api/project /put?callback=jQuery191012879030621062526_1433486934717&models=[{\"ProjectId\":2%2C\"ClientId\":1%2C \"FirmId\":1%2C\"Status\":\"Started\"%2C\"Name\":\"Flexi77\"%2C\"IsActive\":true%2C\"CreatedDate\":\"2015-06-03T00 :00:00\"%2C\"ModifiedDate\":\"2015-06-03T00:00:00\"%2C\"CreatedBy\":\"adnan\"%2C\"ModifiedBy\":\"adnan \"}]&_=1433486934719'.", > "MessageDetail": "No action was found on the controller 'Project' that matches the name 'put'."

json : "No action was found on the controller 'Project' that matches the name 'put'."

Is there some route error or stupid mistake, please do help.... any kind of hint/help is much appreciated.... thanks for your time

1

There are 1 answers

2
JotaBe On BEST ANSWER

The problem is that your Web API action expects two parameters:

  • int id, it's missing from the request URL
  • ProjectsDM projectsdm, can be retrieved from the payload (request body)

You need to use an URL that includes the missing id parameter. In the route template it's optional, but, if it's missing, as the action which requires it, the action cannot be chosen by the action selector.

So either add the id to the URL, or remove the id parameter from the action.