Creating a custom action in an EntitySetController

I have an oData application in .Net 4.5. The oData endpoints work fine, but I want to create a custom action on the controller to set a flag in the client entry when it is given an email address.

Here is my client code (using AngularJS):

    $http({
        method: 'POST',
        url: '/odata/Customers/SetAlertFlag',
        data: { 'email': 'test@test.com'}
    }).success(function (data, status, headers, config) {
        // ...
    }).error(function (data, status, headers, config) {
        // ...
    });

Here is my server code:

public class CustomersController : EntitySetController<Customer, int>
{
    private DBEntities db = new DBEntities();

    // ... other generated methods here


    // my custom method
    [HttpPut]
    public Boolean SetAlertFlag([FromBody] string email)
    {
        return true;
    }
}

The server method is never called. When I type this URL in the browser http://localhost:60903/odata/Customers/SetAlertFlag, I get this error: "An invalid action was detected." SetAlertFlag "is not an action that can be associated with" Collection ([Skirts. Model. Custom Nullable = False]) "."

EntitySetController classes don't have custom methods? How else should I do this?

+4
2

OData POST. :

EDM:

ActionConfiguration action = modelBuilder.Entity<Customer>().Collection.Action("SetAlertFlag");
action.Parameter<string>("email");

:

[HttpPost]
public int SetAlertFlag(ODataActionParameters parameters) 
{ 
    if (!ModelState.IsValid)
    {
        throw new HttpResponseException(HttpStatusCode.BadRequest);
    }

    string email = (string)parameters["email"];
}

:

POST http://localhost/odata/Customers/SetAlertFlag
Content-Type: application/json

{"email":"foo"}

: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions

( , , .)

+7

POST AngularJS, [HttpPut] .NET. POST PUT .

0

Source: https://habr.com/ru/post/1502711/


All Articles