我希望我的MVC应用程序检查(在每个请求中)当前用户是否有配置文件。如果找不到配置文件,我想将它们重定向到“配置文件”页面,让它们提交一个。
protected void Application_AuthenticateRequest()
{
if (HttpContext.Current.User != null)
{
// Redirect to profile page if the current user does not have a profile
if (!HttpContext.Current.User.HasProfile())
Response.RedirectToRoute("Profile");
}
}
我已经扩展了iprincipal以包含一个方法“user.has profile()”,以检查用户是否有配置文件。它起作用了,
但问题是,每个请求都会调用应用程序的authenticateRequest,包括javascript、css等。
此外,当我尝试响应时,它会创建一个重定向循环。
我找到的唯一方法是在重定向到配置文件页之前将以下内容添加到if语句中:
!HttpContext.Current.User.HasProfile() && Request.Path != "/system/profile" && !Request.Path.Contains(".")
它检查当前路径是否不是配置文件页(以防止重定向循环),以及URL中是否有句点(以允许继续加载javascript和css资源)。
有更好的方法吗?你们是怎么处理的?