RedirectToLocal not found

本文关键字:found not RedirectToLocal | 更新日期: 2023-09-27 17:58:58

我有这个代码:

using Solutionsecurity.web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Solutionsecurity.web.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Login() { 
        return View(new User());
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(User u, string ReturnUrl) {
            if (Membership.ValidateUser(u.login, u.password))
            {
                return RedirectToLocal(ReturnUrl);
            }
            else {
                return View(u);
            }
        }
         [Authorize]
        public ActionResult Common()
        {
            return View();
        }
    }
}

我不知道为什么找不到RedirectToLocal!!!这一行:

return RedirectToLocal(ReturnUrl);

有什么想法吗?

RedirectToLocal not found

RedirectToLocal不是框架的一部分。它被添加到Account Controller中的一些MVC模板中:

这取自MVC5模板AccountController:

private ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}

如果你想在所有控制器中使用此方法,那么你可以很容易地将其添加为基本控制器中的受保护方法,并让所有控制器从该基本控制器继承:

public abstract class BaseController : Controller
{
    protected ActionResult RedirectToLocal(string returnUrl)
    {
        if (Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }
}
public class HomeController : BaseController
{
    // ...
}