您好,我目前正在从事实施REST API的项目。作为这种情况下的客户端应用程序,我希望在REST调用中具有嵌入式链接。例如,以下是用于获取在Web API C#-
中编写的ShopController的商店列表的类[HttpGet]
public List<ShopViewModel> TrendingShops()
{
List<ShopViewModel> trendingShops;
using (Entities entities = new Entities())
{
try
{
trendingShops = (from shops in entities.Shops
join reviews in entities.ShopReviews on shops.ShopID equals reviews.ShopID
join images in entities.ShopImages on shops.ShopID equals images.ShopId
select
new ShopViewModel()
{
ShopName = shops.ShopName,
Review = reviews.Review,
Description = shops.Description,
SecondaryDescription = shops.SecondaryDescription,
Rating = reviews.Rating,
ReviewId = reviews.ReviewID,
ShopID = shops.ShopID,
ImageID=images.ImageID,
ImageName=images.ImageName,
ImagePath=images.ImagePath
}).
ToList();
}
catch (Exception ex)
{
trendingShops = null;
throw;
}
}
return trendingShops;
}
下面是ShopViewModel-
public class ShopViewModel
{
public int ShopID { get; set; }
public string ShopName { get; set; }
public long ReviewId { get; set; }
public string Review { get; set; }
public Nullable<decimal> Rating { get; set; }
public String Description { get; set; }
public String SecondaryDescription { get; set; }
public int ImageID { get; set; }
public string ImagePath { get; set; }
public string ImageName { get; set; }
}
此代码给出以下响应
[
{
"ShopID": 1,
"ShopName": "Shop/Product Name 1",
"ReviewId": 2,
"Review": "Good",
"Rating": 3,
"Description": "Shop or Product description comes here",
"SecondaryDescription": "Second line description",
"ImageID": 1,
"ImagePath": "IMGURL",
"ImageName": "TPImage2"
},
{
"ShopID": 2,
"ShopName": "Shop/Product Name 2",
"ReviewId": 3,
"Review": "Good",
"Rating": 4,
"Description": "Shop or Product description comes here",
"SecondaryDescription": "Second line description",
"ImageID": 2,
"ImagePath": "IMGURL",
"ImageName": "TPImage2"
}
]
现在,无需返回服务器获取上述商店之一的商店详细信息。我想将其包含在上面的响应中。
我想知道最好的方法。请注意,我已经在互联网上搜索了“ HATEOAS”
https://msdn.microsoft.com/en-us/magazine/jj883957.aspx
以下要求的链接是否正确?如果没有,请提出正确的方法。
了解在设计REST API时应在何时何地使用HATEOAS原理也将是一件好事。