开发者

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

开发者 https://www.devze.com 2023-03-09 04:44 出处:网络
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.

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 Ac开发者_运维知识库tionResult 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");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消