Hi,
I have project where web api uses attribute routing and somehow rest binding doesn't work on my iggrid.
On other project without attribute routing it works and update method is called after editing ended.
What's the problem here? I'm getting 404 PUT localhost:53300/.../8281452
//controller // PUT api/values/5
[Route(Save)]
[HttpPut] public void Save (int id, [FromBody]Item value) { }
//js
$(function () { var dataURL = "">localhost:53300/.../values"; $("#grid1").igGrid({ dataSource: dataURL, primaryKey: "TIDnumber", restSettings: { update: { url: dataURL }, remove: { url: dataURL, batch: true }, create: { url: "dataURL", batch: true } }, autoGenerateColumns: false, height: "350px", width: "800px", columns: [ { headerText: "TIDnumber", key: "TIDnumber", dataType: "number" }, { headerText: "Description", key: "Description", dataType: "string" }, { headerText: "Quantity", key: "Quantity", dataType: "string" } ], features: [ { name: "Updating", editMode: 'row', editCellStarted: onEditCellStarted, editRowEnded: onEditCellEnded, columnSettings: [{ columnKey: 'TIDnumber', readOnly: true }, { columnKey: "Description", editorType: 'string', validation: true, editorOptions: { required: true } } ] } ] });
});
thank you
Hello Robson,
Thank you for posting in our forum.
Could you let me know what is the route you have defined for the Save method? -> [Route(Save)] . Is this a custom route with a different format than the convention-based routing?
By default the grid on update will make a PUT request to the specified update url in the following format: updateUrl/{id}, which should match the conventional global web api route - "api/{controller}/{id}".
In this case if the specified url is : “localhost/values”, then the request will be send to “localhost/values/<RowID>”. In case you have specified a different route via attribute routing then it is possible for the grid request to no longer match it.
If you wish to change the url template that the grid uses to build its request url you can set the restSettings.update.template option - https://www.igniteui.com/help/api/2019.1/ui.iggrid#options:restSettings.update.template, so that it matches the specified custom route you have defined for your Save method. You can read more on how to set custom urls in the docs: https://www.igniteui.com/help/iggrid-rest-updating#custom-url
Let me know if that solves your issue.
Regards,
Maya Kirova
Hi Maya,
I have standard routing config, no custom routes:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
but I have [RoutePrefix("api/values")] defined
so on update method is only [Route(Save)]
I tried to directly put whole update url in restSetting but didn't work, 404 getting
Thank you for the additional information.
Since a custom route is set for the controller the URI that matches this controller is now changed.
It will now be “api/values/Save” (prefixed with “api/values” from the RoutePrefix, and “Save” form the Route). Note that with the current route template (“Save”) the additional param {id}, which is needed to update the correct row by its id, is not defined and as such will not be passed to the controller. Consider changing the template so that it includes the id as part of the template so when send from the client it can be resolved and passed to the controller.
For example: “Save/{id}”
This would match requests in the following format: api/values/Save/<RowID>
To ensure the grid sends requests in the same format, make sure to set the update url correctly, for example:
update: { url: “/api/values/Save” }
You can read more about attribute routing and how it builds the URIs here: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
And you can also read about the grid rest settings that determine the url the request are send to here: https://www.igniteui.com/help/iggrid-rest-updating
Let me know if you have any additional questions or concerns.
BTW I tried template option as well and didn't work
sorry, this was typo, of course it's: : [Route("Save")].
as long as I put even empty Route[""] api update controller is not called
and getting error 405 , PUT not allowed or 404,
any example how does it work with Attribute Routing?
with ["HttpPut"] alone works fine
Typically the Route attribute accepts a string, which is the URI template for the route. For example:
[Route("api/values")]
In your case you are passing something called “Save”: [Route(Save)]. Could you let me know what the value of Save is?
I’m looking forward to your reply.
Maya