Exemplo n.º 1
0
        internal static EnvelopDto <TDto> CreateLinksForSingleResource <TDto>(TDto dto, HateoasScanner filterConfiguration, LinkGenerator linkGenerator, Type controllerType)
            where TDto : IIdentityDto
        {
            var envelop = new EnvelopDto <TDto>(dto);

            ControllerAction[] actionsFromController = filterConfiguration.ControllerInfoDictionary[controllerType]
                                                       .ControllerActions.Where(t => t.ResourceType.Equals(ResourceType.Single)).ToArray();
            foreach (var action in actionsFromController)
            {
                if (action.MethodType.Equals("Get") || action.MethodType.Equals("Patch"))
                {
                    envelop.Links.Add(new LinkDto(linkGenerator.GetPathByAction(action.ActionName, controllerType.Name.Replace("Controller", ""), new { dto.Id }),
                                                  action.MethodName, action.MethodType));
                }
                else
                {
                    envelop.Links.Add(new LinkDto(linkGenerator.GetPathByAction(action.ActionName, controllerType.Name.Replace("Controller", "")),
                                                  action.MethodName, action.MethodType));
                }
            }
            return(envelop);
        }
Exemplo n.º 2
0
        public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            var result = context.Result as ObjectResult;

            if (FiltersHelper.IsResponseSuccesful(result))
            {
                TDto dto = result.Value as TDto;
                if (filterConfiguration.SupportsCustomDataType && FiltersHelper.GetValueFromHeader(context, "Accept").Equals(filterConfiguration.CustomDataType))
                {
                    var dtoWithLinks = HateoasHelper.CreateLinksForSingleResource(dto, filterConfiguration, linkGenerator, context.Controller.GetType());
                    var envelopDto   = new EnvelopDto <TDto>(
                        dto,
                        dtoWithLinks.Links);
                    result.Value = envelopDto;
                }
                else
                {
                    result.Value = dto;
                }
            }
            await next();
        }