禁用包装控制器结果 [英] Disable Wrapping of Controller Results

查看:138
本文介绍了禁用包装控制器结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Abp.AspNetCore v3.2.5.

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

我正在尝试将Microsoft.AspNetCore.OData的Alpha包集成到该项目中,到目前为止看起来还不错.

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

但是,当我尝试查询元数据控制器时 http://localhost:51078/odata/v1/ $ metadata将结果包装起来. 现在这对于ODataControllers也是一个问题,但是我可以简单地添加 [DontWrapResult]属性.

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.

我没有直接访问MetadataController的权限,所以我无法添加属性.反正有为Abp项目禁用换行吗?

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?

谢谢

修改

这是当前的ConfigureServices方法

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;
}

推荐答案

您可以实现IResultFilter并将WrapOnSuccess设置为false:

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;
    }
}

然后在Startup类中,在ConfigureServices方法中添加过滤器:

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

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

参考:

这篇关于禁用包装控制器结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆