Home > Net >  how can I from outside fonction redirect the user to an other controller in .net?
how can I from outside fonction redirect the user to an other controller in .net?

Time:06-07

Hello every one I’m new in .net and I’m having a problem to figure out how to Redirect to another controller ,what I mean by that :

I have the first controller where I have an action

public IActionResult Create()
        {
            
            RoleController1 Verifiyer = new RoleController1(_context);
            verificateur.role("Personnel_Create",HttpContext.Session.GetString("Role"));

            return View();
        }

what this action should do it’s to verify if the user have the write to access this page using the function role what should this function do : to verify if the user is permitted to enter the page → if yes : enter → and if not I SHOULD redirect him and here where I stuck

I created this class RoleController1 and I’ve defined the function role :

public void  role(string page,string role)
        {
          //the first argument it’s the page and the second is the role   
    
            if (role == "Admin")
            {
              
            }
            else if (role == "Cashier")
            {
                //here I see if he is allowed or not 
                string[] pages = { ""};
                bool exists = Array.Exists(pages, element => element == page);
                if (!exists)
                {
                    //he is not allowed than he should be redireced to the login page 
                    

                    RedirectToAction("login", "Login");     //how to make this work 
                }
            }
            else if(role == "")
            {

            }



        }

I want this function to stop the execution of the Create action and redirect me to the login page

thank you so much for helping me , and ask me please for any further clarification

CodePudding user response:

You should use as return RedirectToAction("actionName","controllerName");.

Method return value should be IActionResult. Like:

public IActionResult Role(string page,string role)
{
   // Your if statements and such.
   return RedirectToAction("actionName","controllerName");
}

CodePudding user response:

I suggest there is no need to directly put the redirect to action inside the role method.

I suggest you could firstly check the role, after checking if it match then return null, if it doesn't match return the RedirectToAction to Create method.

More details, you could refer to below codes:

    public IActionResult Role(string page, string role)
    {
        //the first argument it’s the page and the second is the role   

        if (role == "Admin")
        {
            return null;
        }
        else if (role == "Cashier")
        {
            //here I see if he is allowed or not 
            string[] pages = { "" };
            bool exists = Array.Exists(pages, element => element == page);
            if (!exists)
            {
                //he is not allowed than he should be redireced to the login page 


               return RedirectToAction("login", "Login");     //how to make this work 
            }
            else
            {
                return null;
            }
             
        }
        else if (role == "")
        {
            return null;

        }
        else
        {
            return null;

        }

    }

Then at Create, you could check the method is null or not.

  • Related