找不到RedirectToLocal

时间:2014-04-18 12:05:50

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

我有这段代码:

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);

有什么想法吗?

1 个答案:

答案 0 :(得分:35)

RedirectToLocal不是框架的一部分。它被添加到帐户控制器的一些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
{
    // ...
}