Reputation: 39
hello i want to redirect to a page from _layout.htmlcs using razor view....
I have tried many things to make response.Redirect work but failed...
<div class="navbar-collapse collapse ">
<ul class="navbar-nav flex-grow-1">
@if (User.Identity.IsAuthenticated){
@if (User.IsInRole("Admin")){
Response.Redirect("~/Pages/Admin/AddAccount");
}
if (User.IsInRole("User")){
Respnse.redirect may work properly or its alter native must be used ...
Upvotes: 0
Views: 808
Reputation: 29222
A view is just intended to render a page. It's presumed that if you get to this step, it's already been determined that this is the page you want to render.
If you want to redirect then you would return a RedirectResult
instead of a ViewResult
from your controller method. There are a few methods that return a RedirectResult
like
Redirect(someUrl);
RedirectToAction(actionName, controllerName)
...and others, and overloads of these.
I recommend RedirectToAction
because even though it will compile if you get the values wrong, Visual Studio at least gives you a visual indication that the controller and action you're referring to exist.
Whatever conditions you're checking in the view to determine if you want to redirect, just check them in the controller instead.
Upvotes: 2