Étant donné que la chaîne est nullable, ces surcharges sont vraiment ambiguë du point de vue de MVC. Vérifiez simplement si b
est null (peut-être en faire un paramètre facultatif, si vous souhaitez une valeur par défaut).
Vous pouvez, en revanche, essayer une implémentation personnalisée ActionMethodSelectorAttribute
. Voici un exemple:
public class ParametersRequiredAttribute : ActionMethodSelectorAttribute
{
#region Overrides of ActionMethodSelectorAttribute
/// <summary>
/// Determines whether the action method selection is valid for the specified controller context.
/// </summary>
/// <returns>
/// true if the action method selection is valid for the specified controller context; otherwise, false.
/// </returns>
/// <param name="controllerContext">The controller context.</param><param name="methodInfo">Information about the action method.</param>
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
var parameters = methodInfo.GetParameters();
foreach (var parameter in parameters)
{
var value = controllerContext.Controller.ValueProvider.GetValue(parameter.Name);
if (value == null || string.IsNullOrEmpty(value.AttemptedValue)) return false;
}
return true;
}
#endregion
}
Utilisation:
[ParametersRequired]
public string Download(string a,string b)
// if a & b are missing or don't have values, this overload will be invoked.
public string Download(string a)
Avez-vous réellement * besoin * les deux méthodes? S'il vous plaît donner plus de contexte. –