[英]Different start layouts for different user types with asp.net mvc
这些类型的决策最好在Controller中进行。
例:
public HomeController: Controller
{
public ActionResult Administration()
{
// Determine the user's role.
// "GetRole()" does not really exist on the controller - use your own method.
string role = GetRole();
if (role == "Billing Guy")
return View("AdministrationBillingGuy")
else if (role == "SalesGuy")
return View("AdministrationSalesGuy")
else
return View();
// etc.
}
}
我可以想到几种方法来做到这一点。
如果你需要所有用户获得相同的网址/操作,那么你可以做这样的事情
public ActionResult Custom(RoleEnum userRole)
{
switch(userRole)
{
case RoleEnum.Admin:
.....
return Partial("_adminPartial", viewModel);
// rest of you cases here
}
}
要么:
public ActionResult Custom(RoleEnum userRole)
{
var view = GetViewByRole(userRole);
// where GetViewByRole takes the enum and
// returns a string with the name of the partial
return Partial(view, viewModel);
}
另一种方法是,我建议的是为每个需要不同布局的用户创建一个MVC Area
然后在登录时你可以将它们重定向到适当的Area
,我推荐它,因为它允许更深层次区分UI层中的角色。
实现不同布局的另一种方法(我在谈论类似于ASP.Net Master pages
MVC Layout Pages
ASP.Net Master pages
)是使用ViewBag
或您喜欢的任何其他方法将string Layout
传递给视图,然后在Razor
代码中执行这样的事情:
@model MyViewModel
@{
Layout = (string)ViewBag.Layout;
}
我把这最后一个留给了最后一个,因为它对我来说似乎有些笨拙 。 希望这对你有所帮助
好吧,你没有提供足够的信息给出任何明确的指示,但一般来说,你应该改变你的登录帖子动作,重定向到一个不同的地方,取决于一些识别因素,如角色(以下是伪代码)
// do login
if (user is "Billing")
{
// redirect to billing action
}
// etc.
您应该切换部分或视图的唯一原因是您正在执行SPA(单页面应用程序)并使用JavaScript进行路由。 在这种情况下,您只需要一些可以用AJAX命中的端点来获取用户的“角色”。
但是,我认为这不是你真正在做的事情。 如果您只是直接使用MVC,那么您应该实际更改URL,而不仅仅是加载不同的Razor视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.