Asp.net mvc 2 mvc2中的模糊动作方法

Asp.net mvc 2 mvc2中的模糊动作方法,asp.net-mvc-2,ambiguous,actionmethod,Asp.net Mvc 2,Ambiguous,Actionmethod,我在MVC2中遇到了一些模糊动作方法的问题。我已尝试实现此处找到的解决方案:,但这只会给我一个“找不到资源”错误,因为它认为我正在尝试调用我不想调用的操作方法。我使用的RequiredRequestValueAttribute类与另一个问题的解决方案中的类完全相同: public class RequireRequestValueAttribute : ActionMethodSelectorAttribute { public RequireRequestValueAttribute(

我在MVC2中遇到了一些模糊动作方法的问题。我已尝试实现此处找到的解决方案:,但这只会给我一个“找不到资源”错误,因为它认为我正在尝试调用我不想调用的操作方法。我使用的RequiredRequestValueAttribute类与另一个问题的解决方案中的类完全相同:

public class RequireRequestValueAttribute : ActionMethodSelectorAttribute
{
    public RequireRequestValueAttribute(string valueName)
    {
        ValueName = valueName;
    }
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        return (controllerContext.HttpContext.Request[ValueName] != null);
    }
    public string ValueName { get; private set; }
}
我的行动方法是:

    //
    // GET: /Reviews/ShowReview/ID

    [RequireRequestValue("id")]
    public ActionResult ShowReview(int id)
    {
        var game = _gameRepository.GetGame(id);

        return View(game);
    }

    //
    // GET: /Reviews/ShowReview/Title

    [RequireRequestValue("title")]
    public ActionResult ShowReview(string title)
    {
        var game = _gameRepository.GetGame(title);

        return View(game);
    }

现在,我正在尝试使用
intid
版本,而它正在调用
string title
版本。

此解决方案假设您必须绝对使用相同的URL,无论您是按id还是按名称选择,并且您的路由设置为从URL向此方法传递值

[RequireRequestValue("gameIdentifier")]
public ActionResult ShowReview(string gameIdentifier)
{
    int gameId;
    Game game = null;
    var isInteger = Int32.TryParse(gameIdentifier, out gameId);

    if(isInteger)
    {
      game = _gameRepository.GetGame(gameId);
    }
    else
    {
      game = _gameRepository.GetGame(gameIdentifier);
    }

    return View(game);
}

更新:根据:“不能基于参数重载操作方法。当使用NoActionAttribute或AcceptVerbsAttribute等属性消除歧义时,可以重载操作方法。”

Hmm。。。有趣的选择。如果找不到更干净的方法,我可能会求助于它。我不知道路由引擎有什么干净的方法可以根据可强制转换为特定类型的参数从两种操作方法中选择一种。也许有人会证明我错了。是的,进一步的研究表明你是对的。谢谢你的帮助!