locked
Return data from ActionfilterExecuted RRS feed

  • Question

  • User-257070954 posted

    Hi All,

              I am writing API in Asp.net Core.  In API, After putting data into databases I am trying to implement ActionExecuted filter to get some response message from the database. So my question is once we received that message from the database how can I return the message as API response message. If I retrieve data from database i want to show data + Message from Action filter

    Expected result

    "message": "Posted successfully",   //i want to come this message  from  ActionFilter
    "items": [
    {
    "id": 2,
    "code": "0002",
    "name": "Name-1",
    },
    {
    "id": 3,
    "code": "0003",
    "name": "Name-2",
    }

    Monday, September 23, 2019 12:40 PM

All replies

  • User753101303 posted

    Hi,

    So it seems you would have to read and update https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.actionexecutedcontext.result?view=aspnet-mvc-5.2#System_Web_Mvc_ActionExecutedContext_Result to enrich this with this additional message property but it means you would get the expected result by intervening at two different levels.

    IMO ActionExecuted is best for handling another aspect which doesn't really impact the response (for example counting API calls to report about activity).

    I would start by looking if https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.ok?view=aspnetcore-2.2 or https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.created?view=aspnetcore-2.2 is not enough. Then you can just check the http status code and use the payload without filtering out this extra message.... Add extra payload information only if it really brings some additional benefit.

    Monday, September 23, 2019 1:24 PM
  • User475983607 posted

    Actions are designed to return response messages like JSON not action filters.  Secondly, it is too late to modify the response the action executed filter because, by definition, the action completed which means it returned the response.

    The standard pattern is querying to database from the action and return whatever formatted JSON message you like.

    Perhaps you are facing a design issue?  Can you explain how your application works and why you are using an action executed filter to solve this programming problem?

    Monday, September 23, 2019 2:11 PM
  • User-257070954 posted

    Hi Mgebhard,

    As per your requirement  Below I explain my requirement

    As I explained my base question I am writing web API in .net core.  we planed to implement language translation in an application.  So my requirement is once I added data to the database through API I want to show respective success or failure message to the user in respective language. These messages we taking from the database table. So we planned to implement  Actionexecuted filter to achieve this.

    I hope you understood my requirement. Please correct me I am in the wrong way

    Tuesday, September 24, 2019 4:30 AM
  • User753101303 posted

    What will you do for an error ? If you have to test for localized messages to understand if your http query worked or failed it will cumbersome while Ajax APIs are filtering out of the box succeeded and failed requests (in which case you can still give some more details if you want)...

    Tuesday, September 24, 2019 7:19 AM
  • User-474980206 posted

    Hi Mgebhard,

    As per your requirement  Below I explain my requirement

    As I explained my base question I am writing web API in .net core.  we planed to implement language translation in an application.  So my requirement is once I added data to the database through API I want to show respective success or failure message to the user in respective language. These messages we taking from the database table. So we planned to implement  Actionexecuted filter to achieve this.

    I hope you understood my requirement. Please correct me I am in the wrong way

    this sounds like a really poor design. most application that support multiple languages use a token system, so that variables can be passed into the translation. this handles the variables may be in a different order in the translated string due to language grammar.

    usually to translate a database query you pass mapped column data to the translation routine. 

    Thursday, September 26, 2019 4:13 PM