禁用包含控制器结果

时间:2017-12-19 14:10:13

标签: aspnetboilerplate

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

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

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

我没有直接访问MetadataController,因此我无法添加该属性。反正有没有为Abp项目禁用包装?

由于

修改

这是当前的ConfigureServices方法

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

2 个答案:

答案 0 :(得分:0)

您可以实施IResultFilter并将WrapOnSuccess设置为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方法添加过滤器:

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

参考文献:

答案 1 :(得分:0)

替代解决方案;完全禁用系统内的WrapResult行为(在核心模块注册时):

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

WrapOnSuccess和WrapOnError标志可以设置为false值。