Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToLocal not found

Tags:

I have this code :

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

I have no idea why the RedirectToLocal is not found !!! in this line :

return RedirectToLocal(ReturnUrl);

Any ideas?

like image 326
Lamloumi Afif Avatar asked Apr 18 '14 12:04

Lamloumi Afif


1 Answers

RedirectToLocal is not part of the framework. It is added in some of the MVC templates in the Account Controller:

This is taken from the MVC5 template AccountController:

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

If you want this method in all of your controllers, then you could easily add it as a protected method in a base controller, and have all of your controllers inherit from that base:

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
{
    // ...
}
like image 84
Dismissile Avatar answered Sep 25 '22 06:09

Dismissile