Friday, November 2, 2007
An AppSettings Equivalent
I've been looking for an equivalent of the AppSettings file in Java and came across the java.util.Properties class. I haven't tried it yet but I'm guessing I've got to explicitly specify the file from which I'm going to read the properties from and save the values to. It's good enough for me.
Episode 6 of Bionic Woman missing
Bionic Woman Episode 6 of Season 1 seems to be missing. Usually the episode comes up in a search on Mininova for download as a torrent on Wednesday or latest by Thursday.
I prefer to download them before watching because I've got a slow connection :-(
I prefer to download them before watching because I've got a slow connection :-(
Method Extensions in C# 3.0
One of the new features in C#.NET 3.0 is method extensions. You can add additional methods, for example, to the String class so you can have a ToEmployee method in it. The only difference between a regular static method and a method extension is the addition of the "this" keyword before the parameter declaration.
Generic software components
When building software, most projects reduce development time by making integrating re-usable components. When building a re-usable component, there are two popular strategies for configuring a generic component to the application - you could either have a configuration file, as most .NET applications do, or you could have all configuration parameters sent in via code through setting properties and calling constructors or other methods.
The essence of building a generic component is that you should think of the component as a breadboard - somebody somewhere would want to do something that you did not intent and the ability to adapt the component to the scenario is what makes a successful component.
The essence of building a generic component is that you should think of the component as a breadboard - somebody somewhere would want to do something that you did not intent and the ability to adapt the component to the scenario is what makes a successful component.
Wednesday, October 31, 2007
Changing AppSettings in .NET
Using appsettings("key")=value . Will change the settings in memory and not in web.config
To change it in both, use this method:
VB:
Private Sub ChangeAppSettings(ByVal key As String, ByVal NewValue As String)
Dim cfg As Configuration
cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
Dim setting As KeyValueConfigurationElement = _
CType(cfg.AppSettings.Settings(key), KeyValueConfigurationElement)
If Not setting Is Nothing Then
setting.Value = NewValue
cfg.Save()
End If
End Sub
C#:
private void ChangeAppSettings(string key, string NewValue)
{
Configuration cfg;
cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
KeyValueConfigurationElement setting = (KeyValueConfigurationElement)cfg.AppSettings.Settings(key);
if ((setting != null)) {
setting.Value = NewValue;
cfg.Save();
}
}
To change it in both, use this method:
VB:
Private Sub ChangeAppSettings(ByVal key As String, ByVal NewValue As String)
Dim cfg As Configuration
cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
Dim setting As KeyValueConfigurationElement = _
CType(cfg.AppSettings.Settings(key), KeyValueConfigurationElement)
If Not setting Is Nothing Then
setting.Value = NewValue
cfg.Save()
End If
End Sub
C#:
private void ChangeAppSettings(string key, string NewValue)
{
Configuration cfg;
cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
KeyValueConfigurationElement setting = (KeyValueConfigurationElement)cfg.AppSettings.Settings(key);
if ((setting != null)) {
setting.Value = NewValue;
cfg.Save();
}
}
Disabling ASP.NET Caching
Most web applications with authentication disable caching so after you logout, you can't click the back button to try and view pages that you shouldn't be accessing.
In ASP.NET, you can use the following code to disable caching
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetNoStore()
There are some other snippets you could try out too. Here's one:
Response.ClearHeaders();
Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1
Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1
Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1
Another one to add directly to the HTML code on your page (this one has been known to not work on Firefox):
meta equiv="CACHE-CONTROL" content="NO-CACHE"
You'll probably want to test the code with all of your target web browsers.
Sunday, October 28, 2007
.NET to Java
The JaCIL compiler can convert .NET assemblies (binaries) to Java byte code. The output is a Java Archive (JAR). You can find more here:
http://www.cs.rit.edu/~atg2335/project/quickstart.php
http://www.cs.rit.edu/~atg2335/project/quickstart.php
Subscribe to:
Posts (Atom)