开发者

MVC 3 -ASP.NET Redirect to Different View

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

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

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


You have 2 choices. 1) Create a standard error view and return this in the 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

else
{
   return RedirectToAction("BadUser");
}


public ViewResult BadUser()
{
     ErrorModel viewModel = new ErrorModel(){Msg="Error"});
     return View("Error", viewModel);
}


See Controller.RedirectToAction Method

http://msdn.microsoft.com/ja-jp/library/system.web.mvc.controller.redirecttoaction.aspx

0

精彩评论

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

关注公众号