【问题标题】:asp.net core 2.1 custom default route per user每个用户的 asp.net core 2.1 自定义默认路由
【发布时间】:2019-01-13 08:20:09
【问题描述】:
我有一个公司内部网站,其中包含使用 asp.net 核心区域逻辑实现的不同区域。
我想将用户重定向到他们选择的主页。
例如:
用户 A:contonso.com/ 显示区域索引invoices
用户B:contonso.com/显示区域索引customers
可能的解决方法:使用通用家庭控制器将用户重定向到适当的区域,但我想知道是否有使用内置路由功能的更通用的解决方案。
目前,我会将这些信息存储在数据库中,但实际上我并不关心如何配置,只要我能够动态地进行路由即可。
文档和谷歌没有说明这个案例。
有什么方法可以使用自定义中间件吗?一些内置支持,还是现有的 nuget-package?
【问题讨论】:
标签:
asp.net-core
asp.net-core-mvc
asp.net-mvc-routing
asp.net-core-2.0
【解决方案1】:
你可以使用这样的动作:
public IActionResult MyIndex()
{
string action = // get action for user
return RedirectToAction(action, "Home")
}
连同标签助手:
<a asp-controller="Home" asp-action="MyIndex">Go somewhere</a>
【解决方案2】:
您可以尝试Middleware 根据您的逻辑重定向请求。
这是 MVC 控制器的演示代码:
app.Use(async (context, next) =>
{
if (context.Request.Path == "/" && context.User.Identity.Name == "test@outlook.com")
{
context.Response.Redirect("Home/About", true);
}
else if (context.Request.Path == "/" && context.User.Identity.Name == "test1@outlook.com")
{
context.Response.Redirect("Home/Contact", true);
}
await next();
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
注意,根据你的Area修改redirect URL和MVC Route