在我们的网站中,我们正在制作类似“GodMode”的内容,当启用此模式时,我们希望数据显示与常规公共网站不同。例如,如果用户访问
http://domain/Home/Index
他会看到常规的公共页面。但如果他访问
,则在相同的浏览器/会话中http://domain/godmode/Home/Index
然后我们想要显示其他信息。为此,我创建了一个静态布尔变量,当我们检测到wish
是否为On时,我设置godmode
。所以,我正在寻找一种方法来做到这一点。任何想法如何设置此变量?或者什么是设置它的最佳位置。所以这两个URL都可以在相同的会话中运行而不会互相中断。
据我所知,Global.asax中的Application_BeginRequest
用于应用程序级别启动,而不是每个URL请求。我没有找到任何相关的替代品。任何人都可以建议一个或者我们是否可以在MVC MapRoute
函数中调用函数?
答案 0 :(得分:1)
ActionFilteR怎么样?我用它们来切换这样的语言:
public class InternationalizationAttribute : ActionFilterAttribute
{
/// <summary>
/// The logger
/// </summary>
private NLog.Logger logger;
private NLog.Logger Logger
{
get
{
if (this.logger == null)
{
this.logger = NLog.LogManager.GetCurrentClassLogger();
}
return this.logger;
}
}
/// <summary>
/// Is called from the ASP.net framework before a method is executed
/// </summary>
/// <param name="filterContext">The filter context.</param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string language = (string)filterContext.RouteData.Values["language"] ?? "de";
string culture = (string)filterContext.RouteData.Values["culture"] ?? "DE";
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
}
catch (Exception ex)
{
//// not supported culture, falling to default
this.Logger.Error(string.Format("Invalid culture '{0}-{1}', could not be set", language, culture), ex);
}
}
}
}
在Maproute功能中:
routes.MapRoute(
"DefaultLocalizedQuestionManagementArea",
"{language}-{culture}/{controller}.aspx/{action}/{id}",
new
{
controller = "Home",
action = "Index",
id = string.Empty,
language = "de",
culture = "DE"
});
最后,每个控制器都应该使用这样的属性作为前缀:
[InternationalizationAttribute]
public class AccountController : Controller
{
}