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 
  • 相关阅读:
    Linux中使用expect脚本实现远程机器自动登录_linux shell
    Linux: ssh命令 远程登录
    总结:PgSql备份pg_dump与还原pg_restore
    停电后,在UPS电源下服务器自动关机脚本
    总结:修改相关postgres用户密码
    问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
    delphi 播放mp3
    delphi 获取文件路径函数
    Delphi XE2 使用dbExpress连接MySQL数据库
    kbmmemsql 查询
  • 原文地址:https://www.cnblogs.com/dxh0535/p/10417926.html
Copyright © 2011-2022 走看看