Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

DynamicApiControllerBuilder - Using custom action filter #532


User avatar
0
jaq316 created

I have an application service that is called by an external service and this service requires the response to be a specific XML format. How can I add a custom action filter to return the correct XML response?

My code currently looks like this:

DynamicApiControllerBuilder
                .For<ISomeAppService>("app/some")
                .ForMethod("Process")
                .WithVerb(Abp.Web.HttpVerb.Get)
                .WithFilters(/* ??? How do I modify the response using a custom action filter? *?)
                .Build();

4 Answer(s)
  • User Avatar
    0
    jaq316 created

    40 views but no response?

  • User Avatar
    0
    hikalkan created
    Support Team

    Maybe no one knows :)

    I don't know how to make it with filters. Can you create such a filter for regular Web API controllers (out of ABP)?

  • User Avatar
    0
    jaq316 created

    For anyone that wants to do the same... Here is the solution:

    1. Create the filter class:
    public class PlainTextResponseActionFilter : ActionFilterAttribute, IActionFilter, IFilter
        {
    
            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
                var abpAjaxContent = actionExecutedContext.Response.Content as ObjectContent<Abp.Web.Models.AjaxResponse>;
                if (abpAjaxContent != null)
                {
                    var previousValue = abpAjaxContent.Value as Abp.Web.Models.AjaxResponse;
                    actionExecutedContext.ActionContext.Response.Content = new StringContent(previousValue.Result.ToString());
                }
                else
                    base.OnActionExecuted(actionExecutedContext);
            }
    
        }
    
    1. Reference the class in the WithFilters method of the DynamicApicontrollerBuilder
    DynamicApiControllerBuilder
                    .For<ISomeAppService>("app/some")
                    .ForMethod("Process")
                    .WithVerb(Abp.Web.HttpVerb.Get)
                    .WithFilters(new PlainTextResponseActionFilter())
                    .Build();
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Thank you for this valuable information sharing :)