Tag Archive: c#


Recently I joined a new website hosting provider who supported WebMatrix and MVC. After I used Web Deploy to upload my working files to the server, I got 404 errors on some of the pages. After some investigation I realized that I had to specify the full cshtml file extension on all links. After doing some research, Rick Strahl once again saved my life. In his blog entry, he explained that the following line of code must be added to web.config, if hosted on IIS 7, for routing to work.

<modules runAllManagedModulesForAllRequests="true">

Who would have thought of that?

Advertisements

The following example demonstrates how to disable anonymous authentication in IIS, and enable windows authentication.


using (ServerManager serverManager = new ServerManager())
{
Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration();
Microsoft.Web.Administration.ConfigurationSection anonymousAuthenticationSection = config.GetSection
("system.webServer/security/authentication/anonymousAuthentication", webSiteName + "/" + applicationName);
anonymousAuthenticationSection["enabled"] = false;
Microsoft.Web.Administration.ConfigurationSection windowsAuthenticationSection = config.GetSection
("system.webServer/security/authentication/windowsAuthentication", webSiteName + "/" + applicationName);
windowsAuthenticationSection["enabled"] = true;
serverManager.CommitChanges();
}

Very often, I want to disable the X button on a windows form so I can control what happens when a user closes the form. The following code has been working great for dialogs.


        [DllImport("user32.dll")]
        public static extern int ExitWindowsEx(int uFlags, int dwReason);

        [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
        private static extern IntPtr GetSystemMenu(IntPtr hwnd, int revert);

        [DllImport("user32.dll", EntryPoint = "GetMenuItemCount")]
        private static extern int GetMenuItemCount(IntPtr hmenu);

        [DllImport("user32.dll", EntryPoint = "RemoveMenu")]
        private static extern int RemoveMenu(IntPtr hmenu, int npos, int wflags);

        [DllImport("user32.dll", EntryPoint = "DrawMenuBar")]
        private static extern int DrawMenuBar(IntPtr hwnd);

        private const int MF_BYPOSITION = 0x0400;
        private const int MF_DISABLED = 0x0002;

        public MainForm()
        {
            InitializeComponent();

            IntPtr hmenu = GetSystemMenu(this.Handle, 0);
            int cnt = GetMenuItemCount(hmenu);

            // remove 'close' action
            RemoveMenu(hmenu, cnt-1, MF_DISABLED | MF_BYPOSITION);

            // remove extra menu line
            RemoveMenu(hmenu, cnt - 2, MF_DISABLED | MF_BYPOSITION);

            DrawMenuBar(this.Handle);
        }

However, the above code doesn’t work when the form is the main window of the program. So, I find the following method more robust.


        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                const int CS_NOCLOSE = 0x200;
                cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
                return cp;
            }
        }