MVC 2 中不明确的操作方法

发布于 2024-11-03 03:12:51 字数 1247 浏览 0 评论 0原文

我在 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版本,而不是调用字符串标题版本。

I'm having some problems with ambiguous action methods in MVC 2. I've tried implementing the solution found here: ASP.NET MVC ambiguous action methods, but that simply gives me a "The resource cannot be found" error as it thinks I'm trying to invoke the action method I don't want to invoke. The RequiredRequestValueAttribute class I'm using is the exact same one as what was in the other question's solution:

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; }
}

My action methods are:

    //
    // 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);
    }

Right now, I'm trying to use the int id version, and instead it's invoking the string title version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-11-10 03:12:51

此解决方案假定您必须绝对使用相同的 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);
}

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

This solution assumes that you must absolutely use the same URL regardless of if you're selecting by ID or name, and that your route is set up to pass a value to this method from the 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);
}

Update: According to Microsoft: "Action methods cannot be overloaded based on parameters. Action methods can be overloaded when they are disambiguated with attributes such as NonActionAttribute or AcceptVerbsAttribute."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文