Microsoft MVP Summit, 2007
Bu gece Seattle’a, MVP Summit’e doÄŸru uçuyor olacağım. FotoÄŸrafları yakında burada görebilirsiniz.
Bu gece Seattle’a, MVP Summit’e doÄŸru uçuyor olacağım. FotoÄŸrafları yakında burada görebilirsiniz.
GeliÅŸtirdiÄŸiniz bir Web Custom Control’ünün bir resim dosyasına (resim haricindeki dosya tiplerinin de bu ÅŸekilde kullanılabilir) ihtiyacı olduÄŸunu düşünün. Eminim ki hiçbiriniz bu dosyayı ilgili kontrolü kullandığı web projesine kopyalamakla uÄŸraÅŸmak istemezsiniz. Bu dosyanın var olup olmadığını kontrolün Load event’inde kontrol edebilir ve gerekirse dosyayı DLL içerisinden okuyup bir klasöre kaydedebilirsiniz.
ÖrneÄŸin aÅŸağıdaki kod “spacer.gif” dosyasının “Images” klasöründe olup olmadığını kontrol ediyor ve eÄŸer bu dosya yerinde deÄŸilse, dosyayı DLL içerisinden alıp Images klasörüne kaydediyor.
string m_FilePath = "~/Images/spacer.gif";
if ( ! File.Exists(this.Page.Server.MapPath(m_FilePath)))
{
System.IO.Stream m_Stream = this.GetType().Assembly.GetManifestResourceStream("MyCustomControlProject.ImageResources.spacer.gif");
byte[] m_Buffer = new byte[m_Stream.Length];
int m_BufferSize = Convert.ToInt32(m_Stream.Length);
m_Stream.Read(m_Buffer, 0, m_BufferSize);
System.IO.FileStream m_FileStream = new System.IO.FileStream(this.Page.Server.MapPath(m_FilePath), System.IO.FileMode.Create);
m_FileStream.Write(m_Buffer, 0, m_BufferSize);
m_FileStream.Close();
}
Burada dikkat etmeniz gereken en önemli nokta “GetManifestResourceStream” methoduna vereceÄŸiniz arguman. Bu arguman projenin ismini, klasör yapısını ve dosya ismini içermek zorunda. Benim kullanmış olduÄŸum “MyCustomControlProject.ImageResources.spacer.swf” örneÄŸinde projenin ismi “MyCustomControlProject” ve “spacer.gif” dosyası da “ImageResources” klasörü içerisinde.
Dikkat edilmesi gereken bir diÄŸer nokta ise, bu dosyaya “Solution Explorer” ekranında saÄŸ tıklanmalı, “Properties” menüsüne tıklanmalı ve “Build Action” özelliÄŸi “Embedded Resource” seçeneÄŸi seçilmeli ve proje tekrar build edilmelidir.
Çok basit bir HttpModule örneğini sizlerle paylaşmak isterim.
AÅŸağıdaki kodu yeni bir Class Library projesi oluÅŸturarak, projeye ekleyeceÄŸiniz bir class dosyasına ekleyiniz. Bu projenizin isminin “MyHttpModuleProject” olduÄŸunu var sayıyorum.
using System;
using System.Web;
namespace Sunali
{
public class MyHttpModuleClass : IHttpModule
{
#region CTor
public MyHttpModuleClass()
{
}
#endregion
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(app_BeginRequest);
app.Error += new EventHandler(app_Error);
// Burada HttpApplication class'ının sahip olduğu tüm event'leri tanımlayabilirsiniz.
}
#endregion
#region Event Handlers
void app_BeginRequest(object sender, EventArgs e)
{
HttpApplication m_App = (HttpApplication)sender;
HttpContext m_Context = m_App.Context;
// Örneğin aşağıdaki satır web siteniz üzerindeki tüm sayfalara yazdırılır.
m_Context.Response.Write(string.Format("Mevcut URL: {0}<br /><br />", m_Context.Request.Url));
}
void app_Error(object sender, EventArgs e)
{
HttpApplication m_App = (HttpApplication)sender;
Exception exc = m_App.Server.GetLastError();
// exc deÄŸiÅŸkenine atanan Exception objesini istediÄŸiniz ÅŸekilde loglayabilirsiniz.
}
#endregion
}
}
Ayrıca web sitenizin web.config dosyasına da aşağıdaki ayarı eklemeniz gerekmektedir.
<?xml version="1.0"?>
<configuration>
<system.web>
<httpModules>
<add name="MyModule" type="Sunali.MyHttpModuleClass,MyHttpModuleProject" />
</httpModules>
</system.web>
</configuration>