MVC5 MapRoute的呼叫功能
本文关键字:功能 呼叫 MapRoute MVC5 | 更新日期: 2023-09-27 18:15:59
在我们的网站中,我们正在制作类似"GodMode"的东西,当这种模式启用时,我们希望数据显示与常规公共网站不同。例如,如果用户访问
http://domain/Home/Index
他将看到常规的公共页面。但是在同一个浏览器/会话中,如果他访问
http://domain/godmode/Home/Index
,那么我们想要显示其他信息。为此,我创建了一个静态布尔变量,当我们检测godmode
是否为On时,我将wish
设置。所以,我正在寻找一种方法来做到这一点。知道如何设置这个变量吗?或者放在什么地方最好。所以这两个URL可以在同一个会话中运行而不会被对方打断。
As I know Application_BeginRequest
in Global。asax用于应用程序级启动,而不是每个URL请求。我没有找到任何相关的替代品。谁能建议一个,或者如果我们可以在MVC MapRoute
函数调用一个函数?
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
{
}