Disable Wrapping of Controller Results

╄→尐↘猪︶ㄣ 提交于 2019-12-13 03:38:36

问题


I am currently using v3.2.5 of Abp.AspNetCore.

I am trying to integrate an Alpha package of Microsoft.AspNetCore.OData into the project which is so far looking ok.

However when i try and query the metadata controller http://localhost:51078/odata/v1/$metadata the result is wrapped. Now this was an issue for the ODataControllers as well, but i could simply add the [DontWrapResult] attribute.

I dont have direct access to the MetadataController so i am unable to add the attribute. Is there anyway to disable wrapping for an Abp project?

Thanks

Edit

Here is the current ConfigureServices method

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });

    services
        .AddAuthentication()
        .AddCsDeviceAuth(options => { });

    services
        .AddOData();

    //Configure Abp and Dependency Injection
    var provider = services.AddAbp<PortalWebODataModule>(options =>
    {
        //Configure Log4Net logging
        options.IocManager.IocContainer.AddFacility<LoggingFacility>(
            f => f.LogUsing<Log4NetLoggerFactory>().WithConfig("log4net.config")
        );
    });

    services.Configure<MvcOptions>(options =>
    {
        var abpResultFilter = options.Filters.First(f => f is AbpResultFilter);
        options.Filters.Remove(abpResultFilter);
        options.Filters.AddService(typeof(ODataResultFilter));
    });

    return provider;
}

回答1:


You can implement IResultFilter and set WrapOnSuccess to false:

public class ResultFilter : IResultFilter, ITransientDependency
{
    private readonly IAbpAspNetCoreConfiguration _configuration;

    public ResultFilter(IAbpAspNetCoreConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void OnResultExecuting(ResultExecutingContext context)
    {
        if (context.HttpContext.Request.Path.Value.Contains("odata"))
        {
            var methodInfo = context.ActionDescriptor.GetMethodInfo();

            var wrapResultAttribute =
                GetSingleAttributeOfMemberOrDeclaringTypeOrDefault(
                    methodInfo,
                    _configuration.DefaultWrapResultAttribute
                );

            wrapResultAttribute.WrapOnSuccess = false;
        }
    }

    public void OnResultExecuted(ResultExecutedContext context)
    {
        // No action
    }

    private TAttribute GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
        where TAttribute : class
    {
        return memberInfo.GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
               ?? memberInfo.DeclaringType?.GetTypeInfo().GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
               ?? defaultValue;
    }
}

Then, in Startup class, add the filter in ConfigureServices method:

services.AddMvc(options =>
{
    options.Filters.AddService(typeof(ResultFilter));
});

References:

  • AbpResultFilter.OnResultExecuting
  • ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault



回答2:


Alternative solution; to completely disable WrapResult behavior within the system ( at the Core module registration):

        var abpAspNetCoreConfiguration = Configuration.Modules.AbpAspNetCore();
          abpAspNetCoreConfiguration.DefaultWrapResultAttribute.WrapOnSuccess = false;
          abpAspNetCoreConfiguration.DefaultWrapResultAttribute.WrapOnError = false;
        abpAspNetCoreConfiguration
            .CreateControllersForAppServices(
                typeof(AccessApplicationModule).GetAssembly()
            );

WrapOnSuccess and WrapOnError flags can be set to false values.



来源:https://stackoverflow.com/questions/47888802/disable-wrapping-of-controller-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!