01 September 2005

Firefox recognized as an uplevel browser in ASP.Net

Just paste in <system.web> of web.config or machine.config:


<browserCaps>
<case match="^Mozilla/5\.0 \([^)]*\) (Gecko/[-\d]+)(?'VendorProductToken'
(?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))?">

browser=Gecko
<filter>
<case match="(Gecko/[-\d]+)(?'VendorProductToken'
(?'type'[^/\d]*)([\d]*)/(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*)))">

type=${type}
</case>
<case> <!-- plain Mozilla if no VendorProductToken found -->
type=Mozilla
</case>
</filter>
frames=true
tables=true
cookies=true
javascript=true
javaapplets=true
ecmascriptversion=1.5
w3cdomversion=1.0
css1=true
css2=true
xml=true
tagwriter=System.Web.UI.HtmlTextWriter
<case match="rv:(?'version'(?'major'\d+)(?'minor'\.\d+)(?'letters'\w*))">
version=${version}
majorversion=0${major}
minorversion=0${minor}
<case match="^b" with="${letters}">
beta=true
</case>
</case>
</case>
</browserCaps>

31 August 2005

How to convert from hexadecimal to int?


public string IntToHex(int number)
{
return String.Format("{0:x}", number);
}

public int HexToInt(string hexString)
{
return int.Parse(hexString,
System.Globalization.NumberStyles.HexNumber, null);
}

09 August 2005

Invalidade ASP.Net Cache to reload dependent user controls

If you have two user controls (ascx) in the asp.net cache and need to update a user control if the other changes, you can use the following method:

On the dependent ascx, that needs to refresh:
PartialCachingControl pcc= Parent as PartialCachingControl;
StaticPartialCachingControl spcc= Parent as StaticPartialCachingControl;

if (pcc !=null) pcc.Dependency=new CacheDependency(null, new string[]{"HighLightChange"});

This will create a cache dependency on the HighLightChange string.

On the ascx that needs to invalidate the cache:
HttpRuntime.Cache.Insert("HighLightChange","true");

The change on the HighLightChange will invalidate the cache dependency of the user control that must do a data refresh.

02 August 2005

How to get the ApplicationPath in ASP.Net?

If you have a virtual directory:
string path = HttpContext.Current.Request.ApplicationPath;

If your site is on the root or a virtual directory:

string path = HttpContext.Current.Request.ApplicationPath;
if (path == "/")
path = "http://" + Request.Url.Host;
else
path += "/";

05 May 2005

XML Serialization

This is a sample of how to serialize a class in .Net

using System;

using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace WindowsApplication1
{
public class AppConfig
{
private long _Code;
private string _Description;
private string _Obs;
public long Code
{
get {return _Code;}
set { _Code = value;}
}

public string Description
{
get {return _Description;}
set { _Description = value;}
}

public string Obs
{
get {return _Obs;}
set { _Obs = value;}
}

}

///
/// Summary description for Configuration.
///

[XmlInclude(typeof(AppConfig))]
public class Configuration
{
private AppConfig _AppConfig = null;
public Configuration()
{
_AppConfig = new AppConfig();
}

public void SetConfiguration(long code, string desc, string obs)
{
_AppConfig.Code = code;
_AppConfig.Description = desc;
_AppConfig.Obs = obs;
}

public void Searialize()
{
XmlTextWriter writer = new XmlTextWriter("test.xml", Encoding.Unicode);
writer.Formatting = Formatting.Indented;
XmlSerializer serializer = new XmlSerializer(typeof(AppConfig));
serializer.Serialize(writer, _AppConfig);
writer.Close();
}
}
}