Routing to the actions with same names but different parameters

后端 未结 4 1450
无人共我
无人共我 2021-01-01 11:40

I have this set of routes:

        routes.MapRoute(
            \"IssueType\",
            \"issue/{type}\",
            new { controller = \"Issue\", action         


        
4条回答
  •  一个人的身影
    2021-01-01 12:05

    I would have one Index method that looks for a valid type variable

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

    EDIT:

    How about creating a custom attribute that looks for a specific request value as in this post 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; } 
    } 
    

提交回复
热议问题