简体   繁体   English

如何在中间件中自动生成基于资源的URI以供前端使用?

[英]How to auto-generate resource based URI in middleware for front end to consume?

I'm looking a reusable way to auto-generate URI strings in the middleware and pass them to back to front end for it to use them. 我正在寻找一种可重用的方法来在中间件中自动生成URI字符串,并将其传递回前端,以供其使用。

I want it able the generate uri for any resources that are associated with it. 我希望它能够为与之关联的任何资源生成uri。 Timesheet has a many to one relationship with payrolls. 时间表与工资单具有多对一的关系。

I'm using c# with Entity models, passing JSON to a React front-end. 我在实体模型上使用c#,将JSON传递到React前端。

I want the middleware to generate the URIs that front end will use so that if changes are made to uri structures in the middleware, the front end doesn't need to change how it requests data. 我希望中间件生成前端将使用的URI,以便如果对中间件中的uri结构进行了更改,则前端无需更改其请求数据的方式。

Right now I'm just creating the Uris the resource's class object is populated. 现在,我只是在创建Uris,然后填充资源的类对象。

I've looked into something called a Vanity method, but I don't think that is what I'm be looking for to accomplish. 我已经研究了一种名为“虚荣”方法的方法,但是我认为这不是我想要实现的目标。

What I'm currently using. 我目前正在使用什么。



public int ID
{
   get { return id; }
   set
   {
      id = value;
      updateApis();
   }
}
public string CheckURI { get; set; }
public string TimeSheetURI { get; set; }
public string PayrollURI { get; set; }

private void updateApis()
        {
            CheckURI = "/api/checks?filter=payroll&id=" + ID;
            TimeSheetURI = "/api/timesheets?id=" + ID;
            PayrollURI = "/api/payroll/" + ID;

        }

So when the front end request for a list payrolls like so(this finding payrolls associated with a client id): 因此,当前端这样请求清单工资单时(此查找与客户ID相关联的工资单):

my.site.com/api/payrolls?id=1&pagesize=10&pagenumber=1

It returns a JSON with a list of payroll objects, each with a uri for details on that payroll and any resources front end may need. 它返回一个带有工资单对象列表的JSON,每个对象都带有一个uri,以获取有关工资单和前端可能需要的任何资源的详细信息。 And a uri for more payrolls based on pagination. 和基于分页的更多工资单的uri。

{
  "1":{
        "payrollID":"1",
        "clientID":"1",
        "payrollURI":"/api/payrolls/1",
        "checkURI": "/api/checks?id=1",
        "timesheetURI": "/api/timesheets?id=1"
   },
  "2": {
        "payrollID":"2",
        "clientID":"1",
        "payrollURI":"/api/payrolls/2",
        "checkURI": "/api/checks?id=2",
        "timesheetURI": "/api/timesheets?id=2"
   },
  ...,
  "10": {
        "payrollID":"10",
        "clientID":"1",
        "payrollURI":"/api/payrolls/10",
        "checkURI": "/api/checks?id=10",
        "timesheetURI": "/api/timesheets?id=10"
   },
  "additionalRows":"/api/payrolls?id=1&pagesize=10&pagenumber=2"

}

So I want it to auto-generate payrollURI, checkURI, timesheetURI, with each payrollID. 因此,我希望它使用每个payrollID自动生成payrollURI,checkURI,timesheetURI。 And additionalRows with the original URI information. 以及带有原始URI信息的额外行。

You could use the .Select() method and anonymous objects to transform each timesheet into the formatted object you want. 您可以使用.Select()方法和匿名对象将每个时间表转换为所需的格式化对象。

context.Timesheets.Select(t => new { 
    CheckURI = "/api/checks?filter=payroll&id=" + t.ID,
    TimeSheetURI = "/api/timesheets?id=" + t.ID,
    PayrollURI = "/api/payroll/" + t.ID
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM