代码之家  ›  专栏  ›  技术社区  ›  zerkms

路由到具有相同名称但参数不同的操作

  •  21
  • zerkms  · 技术社区  · 16 年前

    我有这套路线:

            routes.MapRoute(
                "IssueType",
                "issue/{type}",
                new { controller = "Issue", action = "Index" }
            );
    
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
    

    这里是控制器类:

    public class IssueController : Controller
    {
        public ActionResult Index()
        {
            // todo: redirect to concrete type
            return View();
        }
    
        public ActionResult Index(string type)
        {
            return View();
        }
    }
    

    为什么,当我要求的时候 http://host/issue 我得到 The current request for action 'Index' on controller type 'IssueController' is ambiguous between the following action methods:
    我希望第一个方法在没有参数的情况下起作用,第二个方法在指定了某个参数的情况下起作用。

    我在哪里犯了错误?

    UPD :可能重复: Can you overload controller methods in ASP.NET MVC?

    UPD 2 :由于上面的链接-没有任何合法的方法使操作过载,是吗?

    UPD 3 :不能基于参数(c)重载操作方法 http://msdn.microsoft.com/en-us/library/system.web.mvc.controller%28VS.100%29.aspx

    4 回复  |  直到 14 年前
        1
  •  10
  •   Community Mohan Dere    8 年前

    我会有一个索引方法来查找有效的类型变量

        public class IssueController : Controller  
    {  
        public ActionResult Index(string type)  
        {  
            if(string.isNullOrEmpty(type)){
                return View("viewWithOutType");}
            else{
                return View("viewWithType");} 
        }
    }
    

    编辑:

    如何创建一个自定义属性来查找特定的请求值,如本文中所述 StackOverflow

    [RequireRequestValue("someInt")] 
    public ActionResult MyMethod(int someInt) { /* ... */ } 
    
    [RequireRequestValue("someString")] 
    public ActionResult MyMethod(string someString) { /* ... */ } 
    
    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; } 
    } 
    
        2
  •  5
  •   jgreg311    14 年前

    我遇到了一个类似的情况,我希望我的“索引”操作在指定了ID或没有指定ID的情况下处理渲染。我想到的解决方案是使index方法的id参数成为可选的。 例如,我最初尝试两种方法:

    public ViewResult Index()
    {
        //...
    }
    // AND
    public ViewResult Index(int entryId)
    {
        //...
    }
    

    我把它们组合起来,改成:

    public ViewResult Index(int entryId = 0)
    {
        //...
    }
    
        3
  •  1
  •   Ian Mercer    16 年前

    您可以使用actionfilterattribute来执行此操作,该属性使用反射检查参数(我尝试过),但这是一个坏主意。 每个不同的操作都应该有自己的名称。

    为什么不把你的两个方法叫做“索引”和“单一”,比如说,活在命名的限制下呢?

    与在编译时根据匹配的签名绑定的方法不同,末尾缺少的路由值被视为空值。

    如果你想让[hack]actionfilterattribute匹配参数,请告诉我,我会发布一个链接,但就像我说的,这是个坏主意。

        4
  •  1
  •   101010    14 年前

    你所要做的就是用[httppost]标记你的第二个动作。例如:

    public class IssueController : Controller
    {
        public ActionResult Index()
        {
            // todo: redirect to concrete type
            return View();
        }
    
        [HttpPost]
        public ActionResult Index(string type)
        {
            return View();
        }
    }