【发布时间】:2021-02-24 09:16:51
【问题描述】:
ASP.NET Core 有一个有用的类 LinkGenerator,它可以从控制器方法名称生成链接。
但是,这种行为似乎很脆弱,因为它依赖于仅在运行时检查的基于字符串的合约。
如果我更改一个控制器方法名称或一个方法参数名称,程序将表现不佳。
例如:
string url = _linkGenerator.GetPathByAction
(
HttpContext,
action: "GetJobStatus",
values: new { jobId = jobId }
).ToString();
您知道 ASP.NET Core 中强制执行一些编译时间或启动时间检查的功能吗?
比如我想写:
string url = _linkGenerator.GetPathByAction
(
HttpContext,
actionAndValues: () => this.GetJobStatus(jobId), // compile time check (method exists and arguments are compatible)
action: this.GetJobStatus, // at least this: compile time check (method exists)
).ToString();
【问题讨论】:
标签: c# asp.net-core