zoukankan      html  css  js  c++  java
  • 学习笔记4: HATEOAS

    动态类型实现HATEOAS ,提供出超链接等属性,Controller修改一些方法

    1.对于单个资源可以用ExpandoObject

    2.对于集合资源使用匿名类.

    实现:

    1.新建一个类:LinkResource.cs,实现3个属性

        public class LinkResource
        {
            public LinkResource(string href, string rel, string method)
            {
                Href = href;
                Rel = rel;
                Method = method;
            }
    
            public string Href { get; set; }
            public string Rel { get; set; }
            public string Method { get; set; }
        }

    2.在PostController.cs添加方法

    private IEnumerable<LinkResource> CreateLinksForPost(int id, string fields = null)
            {
                var links = new List<LinkResource>();
    
                if (string.IsNullOrWhiteSpace(fields))
                {
                    links.Add(
                        new LinkResource(
                            _urlHelper.Link("GetPost", new { id }), "self", "GET"));
                }
                else
                {
                    links.Add(
                        new LinkResource(
                            _urlHelper.Link("GetPost", new { id, fields }), "self", "GET"));
                }
    
                links.Add(
                    new LinkResource(
                        _urlHelper.Link("DeletePost", new { id }), "delete_post", "DELETE"));
    
                return links;
            }
    
            private IEnumerable<LinkResource> CreateLinksForPosts(PostParameters postResourceParameters,
                bool hasPrevious, bool hasNext)
            {
                var links = new List<LinkResource>
                {
                    new LinkResource(
                        CreatePostUri(postResourceParameters, PaginationResourceUriType.CurrentPage),
                        "self", "GET")
                };
    
                if (hasPrevious)
                {
                    links.Add(
                        new LinkResource(
                            CreatePostUri(postResourceParameters, PaginationResourceUriType.PreviousPage),
                            "previous_page", "GET"));
                }
    
                if (hasNext)
                {
                    links.Add(
                        new LinkResource(
                            CreatePostUri(postResourceParameters, PaginationResourceUriType.NextPage),
                            "next_page", "GET"));
                }
    
                return links;
            }

    其他: [HttpGet("{id}")]  修改为  [HttpGet("{id}",Name="GetPost")

     Controller修改  单笔返回数据

    原代码为:
    
                var postResource = _mapper.Map<Post, PostResource>(post);
    
                var shapedPostResource = postResource.ToDynamic(fields);
    
                return Ok(shapedPostResource);
    修改完成后的代码为:
                var postResource = _mapper.Map<Post, PostResource>(post);
    
                var shapedPostResource = postResource.ToDynamic(fields);
    
                var links = CreateLinksForPost(id, fields);
    
                var result = shapedPostResource as IDictionary<string, object>;
    
                result.Add("links", links);
    
                return Ok(result);

    列表返回数据修改增加连接等参数

    原来是
               var shapedPostResource = postResources.ToDynamicIEnumerable(postParameters.Fields);
    返回 shapedPostResource 
    
    增加:
      var shapedWithLins = shapedPostResource.Select(x =>
                {
                    var dict = x as IDictionary<string, object >;
                    var links = CreateLinksForPost((int)dict["Id"], postParameters.Fields);
                    dict.Add("links", links);
                    return dict;
                });
    
    返回改为 shapedWithLins 
  • 相关阅读:
    java 生成12位随机数,解决The literal 9999999999999 of type int is out of range 问题
    使用loadrunner 12 手动关联
    解析xml文件,修改Jenkins的配置
    自定义报告,用Java写一个html文件
    解决Jenkins的错误“The Server rejected the connection: None of the protocols were accepted”
    selenium项目--读取测试用例
    header("Location:login.php")
    详细设计说明书(转)
    Visual EmbedLinux Tools:让vs支持嵌入式Linux开发(转)
    phpstorm集成phpunit(转)
  • 原文地址:https://www.cnblogs.com/dxh0535/p/10417926.html
Copyright © 2011-2022 走看看