如何通过点击链接从控制器调用方法

1_b*_*bug 0 asp.net razor asp.net-mvc-4

我需要重新发送激活链接给用户,如果他丢失以前的电子邮件与此链接.

所以,我在Login方法中检查了用户状态:

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            bool isConfirmed = (model == null) ? false : WebSecurity.IsConfirmed(model.UserName);
            string errorMsg = "Login or password is incorrect.";

        if (isConfirmed)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }
        }
        else
        {
            if (WebSecurity.UserExists(model.UserName))
            {
                errorMsg = "Your account is not activated. Click <a href=\"" + Url.Content("~/Account/ResendConfirmationLink") + "?id=" + model.UserName +"\" class=\"alert-link\">here</a> for resend email with activation link.";
            }
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", errorMsg);
        return View(model);
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码http://localhost:64612/Account/ResendConfirmationLink?id=nickname通过单击生成链接,我们应该调用以下方法(来自Account控制器):

        [HttpGet]
        public ActionResult ResendConfirmationLink(string id)
        {
            using (ChatContext chatContxt = new ChatContext())
            {
                var tsqlQuery = string.Format("SELECT [ConfirmationToken] FROM [webpages_Membership] WHERE [UserId] IN (SELECT [UserId] FROM [User] WHERE [UserName] LIKE '{0}')", id);
                string confirmationToken = chatContxt.Database.SqlQuery<string>(tsqlQuery).First();
                tsqlQuery = string.Format("SELECT [Email] FROM [User] WHERE [UserName] LIKE '{0}'", id);
                string email = chatContxt.Database.SqlQuery<string>(tsqlQuery).First();
                SendEmail(email, confirmationToken);
            }
            return View("Login");
        }
Run Code Online (Sandbox Code Playgroud)

但方法ResendConfirmationLink()永远不会被调用!重新加载登录视图并将URL转换为http://localhost:64612/Account/Login?ReturnUrl=%2fAccount%2fResendConfirmationLink%3fid%3dusername1&id=username1.

我做错了什么?为什么方法不是通过点击链接调用?

Bra*_*don 5

您需要将[AllowAnonymous]注释添加到您的ResendConfirmationLink操作中.

它希望用户在调用您不想要的方法之前进行身份验证.