我需要第二条路由 /Employees/{EmpID} 在路由为“/Employees/”时触发,而第一条路由在调用 /Employees 时触发。
您可以使用URL Rewriter来重写请求路径。请参阅我下面的演示,其中EmpID
类型int
。
1.创建规则
public class RewriteRuleTest : IRule
{
public void ApplyRule(RewriteContext context)
{
var request = context.HttpContext.Request;
var path = request.Path.Value;
if (path.ToLower() == "/employees/")
{
context.HttpContext.Request.Path = "/employees/0";
}
}
}
2.在启动配置中添加中间件
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseRewriter(new RewriteOptions().Add(new RewriteRuleTest()));
app.UseMvc();
}
3.测试动作
[HttpDelete("/Employees/{EmpID}")]
public void DeleteOne(int empID)
{
if (empID == 0)
{
//for the condition when empID is null or empty
}
else
{
//for the condition when empID is not null or empty
}
}
[HttpDelete("/Employees")]
public void Delete()
{
}