当路径被命名时......
[Route("api/[controller]/{id}/Existing", Name = "ExistingOrdersLink")]
public class OrdersController : Controller {...}
...使用名称
创建绝对路径很容易var address = Url.Link("ExistingOrdersLink", new { id = 22 });
但是,我需要能够从Route名称生成相对路径。所以我写这样的代码:
//Is there a built-in mechanism for this?
string partial = new Uri(Url.Link("ExistingOrdersLink", new { id = id })).PathAndQuery;
以上是有效的,但在ASP.NET Core中,是否有更直接的方法来获得相同的结果?
为什么我需要这个?我制作的一些控制器实际上是通用的:
[Route("api/Subscribers/{id}/[controller]", "SubscriberLink")]
[Route("api/Organization/{id}/[controller]", "OrganizationLink")]
public class OrdersController<T> : Controller where T : IOrder
{
private ICommand<Order<T>> createOrder;
protected OrdersController(ICommand<Order<T>> createOrder)
{
this.createOrder = createOrder;
}
[HttpPost("{otype}")]
public async Task<IActionResult> Create(
Guid id,
[FromBody] Order<T> order)
{
order.CustomerOrderId = Guid.NewGuid();
createOrder.Handle(order);
string[] segs = Request.Path.Value.Split(new char[] { '/' },StringSplitOptions.RemoveEmptyEntries);
//This will become either "SubscriberLink" or "OrganizationLink"
string route = $"{segs[1]}Link";
//Is there a built-in mechanism for this?
string partial = new Uri(Url.Link(route, new { id = id })).PathAndQuery;
partial = $"{partial}/{order.CustomerOrderId}";
CreatedResult reply = Created(partial, null);
return reply;
}
}
答案 0 :(得分:1)
我认为您正在寻找from itertools import imap
with open("map.txt") as f:
rows = imap(str.split, f)
for row in rows:
if row[0].startswith(".data."):
a, b = row[0].rsplit(".", 1)
print(next(rows)[0],a, b)
例如:
Url.RouteUrl