RedirectToLocal not found

前端 未结 1 1854
傲寒
傲寒 2021-02-12 12:39

I have this code :

using Solutionsecurity.web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
         


        
1条回答
  •  执念已碎
    2021-02-12 13:40

    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
    {
        // ...
    }
    

    0 讨论(0)
提交回复
热议问题