2016-08-04 6 views
0

私はASP.Net Web APIプロジェクトのapplication/hal+jsonメディアタイプの応答を生成するのにWebApi.Halを使用しています。インストール・パッケージWebApi.HalWebApi.Halを使用して、生成されたメディアタイプのリンクが空です。

を私は郵便配達を使用して要求を作成したと私は応答以下の受信WebApi.Hal 2.6.0

に概説されているようこれは、以下のナゲットパッケージマネージャのコマンドを使用してプロジェクトにインストールされています。

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}"); } } 
     } 
} 

REFERENCES

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

答えて

0

私は問題を発見した - 私はAcceptヘッダーを使用している必要があります。

サーバーでは、hal + json、hal + xml、および通常のjsonをサポートできます。クライアントはAcceptヘッダーを使用して、どちらが必要かを指定できます。

「Content-Type」は、実際のペイロードのフォーマットを意味します(存在する場合)。要求にはペイロードがないため、Content-Typeは無視されます。

enter image description here

関連する問題