MVC 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
版本,而不是调用字符串标题
版本。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此解决方案假定您必须绝对使用相同的 URL,无论您是按 ID 还是名称进行选择,并且您的路由设置为从 URL 向此方法传递值。
更新:根据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.
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."