Wednesday, July 6, 2016

MVC Utility


Javascript to bottom in master page


@section moduleScript{
     <script src="~/asset/ts/appts/comman/Enums.js"></script>
    <script src="~/asset/ts/appts/comman/ajax.js"></script>
    <script src="~/asset/ts/appts/user/userAuthontication.js"></script>
}

// in master page
  @RenderSection("moduleScript", required: false)


Checkbox is Checked



$('#' + id).is(":checked")

Maintain sessions

 public class CacheVariables
    {
        // first cache variable

        public static TownSiteInfo TownInfo
        {
            get
            {
                System.Web.HttpContext context = System.Web.HttpContext.Current;

                if (context.Cache["CacheTownSiteInfo"] == null)
                {
                    context.Cache["CacheTownSiteInfo"] = new TownSiteInfo(){

                        name="chamith",
                        age = 12
                    };
                }

                return (TownSiteInfo)context.Cache["CacheTownSiteInfo"];
            }
            set
            {
                System.Web.HttpContext context = System.Web.HttpContext.Current;
                context.Cache["CacheTownSiteInfo"] = value;
            }
        }

 // second cachig variable
            public static bool EnableEpurchasingPortal
        {
            get
            {
                System.Web.HttpContext context = System.Web.HttpContext.Current;

                if (context.Cache["EnableEpurchasingPortal"] == null)
                {
                    context.Cache["EnableEpurchasingPortal"] = false;
                }

                return Convert.ToBoolean(context.Cache["EnableEpurchasingPortal"]);

            }
            set
            {
                System.Web.HttpContext context = System.Web.HttpContext.Current;
                context.Cache["EnableEpurchasingPortal"] = value;
            }
        }
    }

useage

CacheVariables.TownInfo.TownName

// application start

 public void Application_Start(object sender, EventArgs e)
        {
            // Fires when the application is started

            // TODO: re-enable if financial is added back to the project
            // FinancialGlobalConfig.TSDateFormat = "MM/dd/yyyy"

            System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("en-CA");
            // TODO: renable if financial is added back to the project
            info.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";
            System.Threading.Thread.CurrentThread.CurrentCulture = info;

            if (System.IO.Directory.Exists(GlobalConfig.ProcomLocalAppDataDirectory) == false)
            {
                System.IO.Directory.CreateDirectory(GlobalConfig.ProcomLocalAppDataDirectory);
            }

            // AUTOMAPPER CONFIGURATIONS
            AutoMapperConfig.Init();
            Trace.CreateLogTable();

            var routeConfig = new Setup.Routes();
            routeConfig.Config();

            // SSL must be configured before any secure (all of them) service calls will work
            var ssl = new Setup.SslCerts();
            ssl.RegisterCertCallback();

            var cache = new Setup.Cache();
            cache.SetupCacheItems();

            App_Start.BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        /////////////


 maintain sessions
 
 // any class file inside project
 public class GlobalConfig
    {

      public static string ProcomLocalAppDataDirectory
        {
            get
            {
                string procomDir = null;
#if LINUX
                 procomDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
#else
                procomDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
#endif
                procomDir = System.IO.Path.Combine(procomDir, "Procom");
                return procomDir;
            }
        }

 public static LibTsDto.UserCredentials UC
        {
            get
            {
                System.Web.HttpContext context = System.Web.HttpContext.Current;
                if (context.Session["Cred"] != null)
                {
                    return (LibTsDto.UserCredentials)context.Session["Cred"];
                }
                return null;
            }
            set
            {
                System.Web.HttpContext context = System.Web.HttpContext.Current;
                context.Session["Cred"] = value;
            }
        }
    }
}

// useage
GlobalConfig.ProcomLocalAppDataDirectory
GlobalConfig.UC.Email

No comments:

Post a Comment

CS Events