2

MVC 2のあいまいなアクションメソッドに問題があります。ここにあるソリューションを実装しようとしました:ASP.NET MVCのあいまいなアクションメソッドですが、それは単に「リソースが見つかりません」というエラーを表示します。呼び出したくないアクションメソッドを呼び出そうとしています。私が使用している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);
    }

現在、私はそのint idバージョンを使おうとしていますが、代わりにそのバージョンを呼び出していstring titleます。

4

1 に答える 1

2

このソリューションは、IDまたは名前で選択するかどうかに関係なく、絶対に同じURLを使用する必要があり、ルートが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);
}

更新:Microsoft によると:「アクションメソッドはパラメーターに基づいてオーバーロードできません。アクションメソッドは、NonActionAttributeやAcceptVerbsAttributeなどの属性で明確にされている場合にオーバーロードされる可能性があります。」

于 2011-04-20T17:32:17.903 に答える