具有相同操作名称的多个操作

时间:2015-02-09 13:17:44

标签: c# asp.net-mvc-4

我的控制器中有多个动作,如此

public ActionResult Verify(String email, String name ){
        ViewBag.email = email;
        ViewBag.name = name;
        return View();
}

[HttpGet]
public ActionResult Verify(String uId){
        User user = TippNett.Core.User.Get(uId);
        user.Active = true;
        user.Save();
        Auth.Authenticate(user, false);
        return RedirectToAction("Index", "Home");
}

第一个操作是当用户注册向他显示注册消息时,请验证电子邮件,我这样称呼它

return RedirectToAction("Verify", "Account", new { email = email, name = user.FirstName});

当用户点击验证链接时,系统会调用第二个操作。 问题在于始终调用以下函数。即使我传递电子邮件和名称作为参数。

任何人都可以解释为什么会发生这种情况并可能解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

您可以使用:

[ActionName("MyOverloadedName")]

<强> method overloading based on attribute:

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

但是,您必须为与 相同的http方法使用不同的操作名称,在使用不同的http方法时,您只能使用相同的方法。 喜欢:

[HttpPost]
public ActionResult Verify(String email, String name ){
      }

[HttpGet]
public ActionResult Verify(String uId){
        User user = TippNett.Core.User.Get(uId);
        user.Active = true;
        user.Save();
        Auth.Authenticate(user, false);
        return RedirectToAction("Index", "Home");
}

答案 1 :(得分:0)

最近我抓住机会进一步完善了@Tushar的伟大RequireRequestValueAttribute,使其支持其他方案,例如多参数支持和触发它的不同类型的匹配:所有给定的参数,任何一个他们甚至没有。

我调用了新版本RequiredParameterAttribute,我在最终使用的所有MVC项目中使用它。

以下是完整的源代码:

/// <summary>
/// Flags an Action Method valid for any incoming request only if all, any or none of the given HTTP parameter(s) are set,
/// enabling the use of multiple Action Methods with the same name (and different signatures) within the same MVC Controller.
/// </summary>
public class RequireParameterAttribute : ActionMethodSelectorAttribute
{
    public RequireParameterAttribute(string parameterName) : this(new[] { parameterName })
    {
    }

    public RequireParameterAttribute(params string[] parameterNames)
    {
        ParameterNames = parameterNames;
        IncludeGET = true;
        IncludePOST = true;
        IncludeCookies = false;
        Mode = MatchMode.All;
    }

    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        switch (Mode)
        {
            case MatchMode.All:
            default:
                return (
                    (IncludeGET && ParameterNames.All(p => controllerContext.HttpContext.Request.QueryString.AllKeys.Contains(p)))
                    || (IncludePOST && ParameterNames.All(p => controllerContext.HttpContext.Request.Form.AllKeys.Contains(p)))
                    || (IncludeCookies && ParameterNames.All(p => controllerContext.HttpContext.Request.Cookies.AllKeys.Contains(p)))
                    );
            case MatchMode.Any:
                return (
                    (IncludeGET && ParameterNames.Any(p => controllerContext.HttpContext.Request.QueryString.AllKeys.Contains(p)))
                    || (IncludePOST && ParameterNames.Any(p => controllerContext.HttpContext.Request.Form.AllKeys.Contains(p)))
                    || (IncludeCookies && ParameterNames.Any(p => controllerContext.HttpContext.Request.Cookies.AllKeys.Contains(p)))
                    );
            case MatchMode.None:
                return (
                    (!IncludeGET || !ParameterNames.Any(p => controllerContext.HttpContext.Request.QueryString.AllKeys.Contains(p)))
                    && (!IncludePOST || !ParameterNames.Any(p => controllerContext.HttpContext.Request.Form.AllKeys.Contains(p)))
                    && (!IncludeCookies || !ParameterNames.Any(p => controllerContext.HttpContext.Request.Cookies.AllKeys.Contains(p)))
                    );
        }
    }

    public string[] ParameterNames { get; private set; }

    /// <summary>
    /// Set it to TRUE to include GET (QueryStirng) parameters, FALSE to exclude them:
    /// default is TRUE.
    /// </summary>
    public bool IncludeGET { get; set; }

    /// <summary>
    /// Set it to TRUE to include POST (Form) parameters, FALSE to exclude them:
    /// default is TRUE.
    /// </summary>
    public bool IncludePOST { get; set; }

    /// <summary>
    /// Set it to TRUE to include parameters from Cookies, FALSE to exclude them:
    /// default is FALSE.
    /// </summary>
    public bool IncludeCookies { get; set; }

    /// <summary>
    /// Use MatchMode.All to invalidate the method unless all the given parameters are set (default).
    /// Use MatchMode.Any to invalidate the method unless any of the given parameters is set.
    /// Use MatchMode.None to invalidate the method unless none of the given parameters is set.
    /// </summary>
    public MatchMode Mode { get; set; }

    public enum MatchMode : int
    {
        All,
        Any,
        None
    }
}

我保留了“旧”签名,因此可以像上一部分一样使用。

有关更多信息和一些实施示例,请查看我就此主题撰写的this blog post

答案 2 :(得分:-1)

您应该使用HttpPost属性,请参阅:

HttpPost vs HttpGet attributes in MVC: Why use HttpPost?

了解更多信息。