使用 WebApi.Hal 生成的媒体类型中的链接为空

Links are empty in generated media type using WebApi.Hal

我正在使用WebApi.Hal to generate application/hal+json media type response from my ASP.Net Web API project. This is installed to the project using following nugget package manager command as outlined in WebApi.Hal 2.6.0

Install-Package WebApi.Hal

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

{
  "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

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

服务器可以支持hal+json、hal+xml和普通json。客户端可以使用 Accept header 来判断它想要哪一个。

"Content-Type" 表示实际负载的格式,如果有的话。该请求没有负载,因此 Content-Type 被忽略。