2016-08-04 11 views
0

Я использую WebApi.Hal, чтобы сгенерировать application/hal+json ответ типа мультимедиа от моего проекта ASP.Net Web API. Это устанавливается в проекте, используя следующий самородок пакет команду менеджера, как указано в WebApi.Hal 2.6.0Ссылки пустые в сгенерированном носителе с использованием WebApi.Hal

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

ответ

0

Я нашел этот вопрос - я должен использовать Accept заголовок.

Сервер может поддерживать hal + json, hal + xml и обычный json. Клиент может использовать заголовок Accept, чтобы сообщить, какой из них он хочет.

«Content-Type» означает формат фактической полезной нагрузки, если таковой имеется. Запрос не имеет полезной нагрузки, поэтому Content-Type игнорируется.

enter image description here