将ASP.NET MVC操作参数名称映射到另一个名称

ela*_*ado 3 c# parameters asp.net-mvc action

我可以将动作的参数映射到其他名称吗?

我想使用保留字作为动作的参数,例如:

search?q=someQuery&in=location&for=x
Run Code Online (Sandbox Code Playgroud)

因此"in"和"for"不能用作方法的参数名称.是否有内置功能或我应该创建模型绑定器?

谢谢.

wom*_*omp 11

您可以使用'@'表示法使名称按字面解释,而不是c#中的保留字.

public ActionResult Test(string @for)
{
    var something = @for;
}
Run Code Online (Sandbox Code Playgroud)

  • public ActionResult MyMethod([Bind(Prefix ="in")] string inLocation){...} (7认同)