ASP.NET MVC5: RedirectToAction

I want that if the user does not have access to a specific page, the redirect to this page will be "ErrorAccessPage.cshtml" . There is no controller on this page. It is located in the name of the General folder .

Here's the logic:

if (user has access){
    return View();
}
else
{
    return RedirectToAction("//how to input the page here?");
}

Update:

After I changed the code to this:

if (moduleViewModel.CanRead == true){
    return View();
}
else
{
    return RedirectToAction("~/Shared/ErrorAccessPage.cshtml");
} 

enter image description here

+4
source share
2 answers

You cannot do RedirectToActionwithout a controller, since Action must be on the controller. However, you can redirect to a "simple" html file:

Redirect("~/Shared/ErrorAccessPage.html");

or you can return the view directly from the current controller action without redirecting:

return View("~/Shared/ErrorAccessPage.cshtml");

, "", MVC . :

:

return View("~/Views/Shared/ErrorAccessPage.cshtml");

MVC - "", :

<add key="webpages:Enabled" value="true" />

web.config

.

+5

View("ErrorAccessPage"), .

RedirectToAction() , . , .

, View("view_name"). html, aspx cshtml View- > Your_Current_Controller_Name View- > Shared .

if (user has access){
    return View();
}
else
{
    return View("ErrorAccessPage");
}

, ,

, .

-1

Source: https://habr.com/ru/post/1610220/


All Articles