In MVC 3-ASP.NET, I am validating the permission level on the page in the controller. If the user is authorised to see the page then I am using the following code to render it but I don't how to redirect to a new view if not authorised
在MVC 3-ASP.NET中,我正在验证控制器页面上的权限级别。如果用户有权查看该页面,那么我使用以下代码进行渲染,但如果未经授权,我不知道如何重定向到新视图
Could any one tell me how to display alert saying, you are not authorised to see the page and redirect to home page?
任何人都可以告诉我如何显示提醒说,您无权查看该页面并重定向到主页?
public ActionResult viewName()
if(userAuthorised)
{
return View()
}
else
{
//Alert Message
//Redirect to different view like Home Page..
}
Any examples please?
有什么例子吗?
Thank you
2 个解决方案
#1
5
You have 2 choices. 1) Create a standard error view and return this in the else:
你有2个选择。 1)创建标准错误视图并在else中返回:
else
{
ErrorModel viewModel = new ErrorModel(){Msg="Error"});
return View("Error", viewModel);
}
2) Use a Redirect to Action which points at another Controller method which returns the Error View
2)使用指向另一个返回错误视图的Controller方法的Redirect to Action
else
{
return RedirectToAction("BadUser");
}
public ViewResult BadUser()
{
ErrorModel viewModel = new ErrorModel(){Msg="Error"});
return View("Error", viewModel);
}
#2
2
See Controller.RedirectToAction Method
请参阅Controller.RedirectToAction方法
http://msdn.microsoft.com/ja-jp/library/system.web.mvc.controller.redirecttoaction.aspx
#1
5
You have 2 choices. 1) Create a standard error view and return this in the else:
你有2个选择。 1)创建标准错误视图并在else中返回:
else
{
ErrorModel viewModel = new ErrorModel(){Msg="Error"});
return View("Error", viewModel);
}
2) Use a Redirect to Action which points at another Controller method which returns the Error View
2)使用指向另一个返回错误视图的Controller方法的Redirect to Action
else
{
return RedirectToAction("BadUser");
}
public ViewResult BadUser()
{
ErrorModel viewModel = new ErrorModel(){Msg="Error"});
return View("Error", viewModel);
}
#2
2
See Controller.RedirectToAction Method
请参阅Controller.RedirectToAction方法
http://msdn.microsoft.com/ja-jp/library/system.web.mvc.controller.redirecttoaction.aspx