Skip to main content

Peter Marshall

Pass an array of int to ASP.NET WEBAPI C#

3 min read

Today I had to provide an endpoint that accepts an array of integers. As it happens this was for a DELETE operation. So an existing edpoint api/link/{id}  exists. The problem was I could not find any guidance for a RESTFUL url for delete a whole bunch of links. Initialy I proposed passing the array in the body, this was rejected as angular was not able to call the DELETE and pass a body.

So, the url is going to look like api/link/2,4,67,4

 

Create a Model to contain the passed in list of int.

public class LinkIds {     public List<int> Ids { getset; }     public List<KeyValuePair<int,bool>> Results { getset; } }

You need to create a model binder that inherits from IModelBinder.

public class LinkIdModelBinder : IModelBinder
{

And that requires a BindModel method.

public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) {   

Within the BindModel method we can validate that the type is correct

if (bindingContext.ModelType != typeof(LinkIds)) {     return false; }

Validate the name.

var val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (val == null) {     return false; }

Validate that we have got a vlaue.

var key = val.RawValue as string; if (key == null) {     bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Wrong value type");     return false; }

check we have integers.

var values = val.AttemptedValue.Split(','); var ids = new List<int>(); foreach (var value in values) {     int intValue;     int.TryParse(value.Trim(), out intValue);     if (intValue > 0)     {         ids.Add(intValue);     } }

Get the passed value into our bindingContext, and return everything fine.

if (ids.Count > 0) {     var result = new LinkIds     {         Ids = ids     };     bindingContext.Model = result;     return true; }

Or if there is an issue report it.

bindingContext.ModelState.AddModelError(     bindingContext.ModelName, "Cannot convert value to list of int."); return false;

 

Now we can create the endpoint in the controller as follows

[HttpDelete]

[VersionRoute("{linkIds}", 1, true)]

[ResponseType(typeof(bool))] public async Task<IHttpActionResult> DeleteAllAsync([ModelBinder(typeof(LinkIdModelBinder))] LinkIds linkIds) {

And that works

linkIds.Ids can be processed as a list.

 

Peter Marshall

Driver A light-weight, no-dependency, vanilla JavaScript engine to drive the user's focus across the page

A light-weight, no-dependency, vanilla JavaScript engine to drive the user's focus across the page

Peter Marshall

Peter Marshall

Peter Marshall

Peter Marshall

Peter Marshall

Peter Marshall

Peter Marshall

Awesome Electron

1 min read

Because I am writing an app in electron. I found this page, to be very very useful. awesome-electron

I am using bozon to get up amd running and provide the scafolding.

 this is the majic launch file that alows debugging in vs code

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Main Process",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
      "windows": {
        "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
      },
      "program": "${workspaceRoot}/main.js",
      "protocol": "legacy"
    },
    {
      "name": "Debug Renderer Process",
      "type": "chrome",
      "request": "launch",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
      "windows": {
        "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
      },
      "runtimeArgs": [
        "${workspaceRoot}/main.js",
        "--remote-debugging-port=9222"
      ],
      "webRoot": "${workspaceRoot}"
    }
  ]
}

Peter Marshall

Now using Brave Browser https://brave.com/index/ with Qwant for search https://www.qwant.com/