简体   繁体   English

MapRoute上的MVC5调用功能

[英]MVC5 Calling function on MapRoute

In our site we are making something like "GodMode", when this mode is enable, we want to have data display differently than regular public website. 在我们的网站上,我们正在制作类似“ GodMode”的内容,启用此模式后,我们希望数据显示与常规公共网站不同。 For example, if a user visit 例如,如果用户访问

http://domain/Home/Index 

He will see regular public page. 他将看到常规的公开页面。 but in same browser/session if he visit 但如果他访问同一浏览器/会话

http://domain/godmode/Home/Index

then we want to show other information. 那么我们想显示其他信息。 For this I create a Static boolean Variable that I wish to set when we detect if godmode is On. 为此,我创建了一个静态布尔变量, wish在检测到godmode是否为On时进行设置。 So, I am looking a way to do this. 因此,我正在寻找一种方法来执行此操作。 Any Idea how can I set this variable? 任何想法我如何设置此变量? Or what is best place to set it. 或设置它的最佳位置。 so both URL can run in Same Session without interrupt from each other. 因此,两个URL都可以在同一会话中运行,而不会互相干扰。

As I know Application_BeginRequest in Global.asax is for Application level start, not per URL request. 据我所知Application_BeginRequest Global.asax中是应用级开始,而不是每个URL请求。 I didn't found any relevant replacement for it. 我没有找到任何相关的替代品。 Can anyone please suggest one or if we can call a function in MVC MapRoute function? 任何人都可以提出一个建议,或者是否可以在MVC MapRoute函数中调用一个函数?

What about an ActionFilteR? 那ActionFilteR呢? I use them to switch languages like this: 我用它们来切换这样的语言:

    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);
            }
        }
    }
}

And in the Maproute function: 并在Maproute函数中:

  routes.MapRoute(
                "DefaultLocalizedQuestionManagementArea",
                "{language}-{culture}/{controller}.aspx/{action}/{id}",
                new
                {
                    controller = "Home",
                    action = "Index",
                    id = string.Empty,
                    language = "de",
                    culture = "DE"
                });

Finally, every controller ha sto be prefixed with the Attribute like this: 最后,每个控制器都必须以Attribute作为前缀:

  [InternationalizationAttribute]
    public class AccountController : Controller
    {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM