使用WebApi.Hal

时间:2016-08-04 02:49:38

标签: rest asp.net-web-api hateoas hypermedia media-type

我正在使用WebApi.Hal从我的application/hal+json项目生成ASP.Net Web API媒体类型响应。使用WebApi.Hal 2.6.0

中列出的以下nugget包管理器命令将其安装到项目中
  

Install-Package WebApi.Hal

我使用邮递员创建了一个请求,我收到了以下回复。

enter image description here

{
  "Id": 1,
  "Name": "blogEntryName",
  "StyleId": 1,
  "StyleName": "StylName",
  "_links": [],
  "_embedded": null
}

这些链接是空的。如何获取链接?

Global.asax中

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            GlobalConfiguration.Configuration.Formatters.Add(new JsonHalMediaTypeFormatter());
            GlobalConfiguration.Configuration.Formatters.Add(new XmlHalMediaTypeFormatter());

        }

控制器和其他类

public class ValuesController : ApiController
    {

        public ValuesController()
        {

        }

        // GET api/values
        public BlogEntryRepresentation Get()
        {

            List<int> reviewIds = new List<int>();
            reviewIds.Add(1);
            reviewIds.Add(2);
            reviewIds.Add(3);

            BlogEntryRepresentation beerRep = new BlogEntryRepresentation
            {
                Id = 1,
                Name = "blogEntryName",

                StyleId = 1,
                StyleName = "StylName",
                ReviewIds = reviewIds
            };

            return beerRep;

        }        

    }

        public class BlogCategory
        {
            protected BlogCategory()
            {
            }

            public int Id { get; protected set; }
            public string Name { get; set; }
        }

        public class BlogEntry
        {
            protected BlogEntry()
            {
            }

            public BlogEntry(string name)
            {
                Name = name;
            }

            public int Id { get; protected set; }
            public string Name { get; set; }
            public BlogCategory Style { get; set; }

        }

        public class BlogEntryRepresentation : Representation
        {
            public int Id { get; set; }
            public string Name { get; set; }


            public int? StyleId { get; set; }
            public string StyleName { get; set; }

            [JsonIgnore]
            public List<int> ReviewIds { get; set; }

            public override string Rel
            {
                get { return LinkTemplates.BlogEntries.BlogEntry.Rel; }
                set { }
            }

            public override string Href
            {
                get { return LinkTemplates.BlogEntries.BlogEntry.CreateLink(new { id = Id }).Href; }
                set { }
            }

            protected override void CreateHypermedia()
            {
                if (StyleId != null)
                    Links.Add(LinkTemplates.BlogCategories.Style.CreateLink(new { id = StyleId }));

                if (ReviewIds != null && ReviewIds.Count > 0)
                    foreach (var rid in ReviewIds)
                        Links.Add(LinkTemplates.Reviews.GetBeerReview.CreateLink(new { id = Id, rid }));
            }
        }

public static class LinkTemplates
    {

        public static class BlogCategories
        {
            public static Link GetStyles { get { return new Link("styles", "~/styles"); } }
            public static Link AssociatedBlogEntries { get { return new Link("blogEntries", "~/styles/{id}/blogEntries{?page}"); } }
            public static Link Style { get { return new Link("style", "~/styles/{id}"); } }
        }

        public static class BlogEntries
        {
            public static Link GetBlogEntries { get { return new Link("blogEntries", "~/blogEntries{?page}"); } }
            public static Link SearchBeers { get { return new Link("page", "~/blogEntries{?searchTerm,page}"); } }
            public static Link BlogEntry { get { return new Link("blogEntry", "~/blogEntries/{id}"); } }
        }

        public static class Reviews
        {
            public static Link GetBeerReview { get { return new Link("review", "~/blogEntries/{id}/reviews/{rid}"); } }
        }
}

参考

  1. Top 20 NuGet packages for hypermedia
  2. Popular C# HAL Projects

1 个答案:

答案 0 :(得分:0)

我发现了问题 - 我应该使用Accept标题。

服务器可以支持hal + json,hal + xml和普通json。客户端可以使用Accept标头来告诉它想要哪一个。

“Content-Type”表示实际有效负载的格式(如果有)。请求没有有效负载,因此将忽略Content-Type。

enter image description here