简体   繁体   中英

MVC-3 ASP.NET Shared Views-Redirect-Razor

I have a shared view called NotAuthorised in the folder 'Views/Shared'. I want to redirect the users to this view when they are not authorised to see the page.

Initially, this view was in a folder called Account. But I moved it into the Shared folder as I am not using the Account anymore. I have deleted the Account folder.

I used the following code to redirect:

public ActionResult NotAuthorised()
{  
   return RedirectToAction("NotAuthorised", "Account");
}

Now that I removed the Account folder, I'm trying to use

public ActionResult NotAuthorised()
{  
   return RedirectToAction("NotAuthorised", "Shared");
}

I am completely wrong by giving the folder name shared in the last line.

Could anyone tell me, what I am doing wrong?

Thank you

You can't redirect to a View , only to an Action of a Controller . You have to specify an controller action for your redirect and there you can render your shared view.

public class AuthorizeController : Controller
{
    public ActionResult NotAuthorised()
    {  
       return View("NotAuthorised");
    }
}

and later redirect to this new action from within any other action method:

return RedirectToAction("NotAuthorised", "Authorize");

But you may not need this additional Controller . You could simply render the shared View

public ActionResult NotAuthorised()
{  
   return View("NotAuthorised");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM