我有两种形式发布到同一URL:http://localhost/foo/bar
<form method="POST" action="http://localhost/foo/bar">
<input name="someId" value="123" />
</form>
<form method="POST" action="http://localhost/foo/bar">
<input name="someData" value="345435435" />
</form>
我在FooController
中有以下操作:
[HttpPost("bar")]
public IActionResult A([FromForm] string someId)
{
...
}
[HttpPost("bar")]
public IActionResult B([FromForm] string someData)
{
...
}
但是我得到一个AmbiguousMatchException
。我想做的事有可能吗?
答案 0 :(得分:1)
我用一个属性解决了它!
public class ParameterConstraint : ActionMethodSelectorAttribute
{
private readonly string _name;
public ParameterConstraint(string name)
{
_name = name;
}
public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action)
{
return !string.IsNullOrEmpty(routeContext.HttpContext.Request.Form[_name]);
}
}
[HttpPost("bar")]
[ParameterConstraint("someId")]
public IActionResult A([FromForm] string someId)
{
...
}
[HttpPost("bar")]
[ParameterConstraint("someData")]
public IActionResult B([FromForm] string someData)
{
...
}