Visual Studio Web Transform, Bu makalemizde belkide sürekli farkında olmadan kullandığımız yada farkında olup sıklıkla kullandığımız web transform durumundan bahsedeceğiz ve web transform işleminin build sırasında nasıl tetikleneceğini göreceğiz.
Visual Studio Web Transform’u Build İle Tetikleme
Normalde web transformlar bir düzenleme yapılmadığı sürece publish durumunda seçmiş olduğunuz configuration’a göre web transform devreye girip web.config dosyasının düzenlenmiş son halini üretir. Peki düzenlenmiş son hali derken neden bahsediyoruz hemen örnekler üzerinden gidelim.
Yeni bir Framework 4.5 sürümlü ASP.NET projesi oluşturduğumuzda Web.config ve 2 configuration olan Debug ve Release configuration’larına göre Web.Debug.config ve Web.Release.config adında toplamda 3 configuration dosyası oluşturuluyor. Paragraf girişinde Framework 4.5 sürümünden açıkça bahsetme nedenim birazdan vereceğim Web.config örnekleri Framework 4.5 sürümlü ASP.NET Empty projesi ile oluşturulduğundan farklı bir sürümle oluşturduğunuzda oluşacak farklılıklar Framework Versiyonuna ve Template türüne göre farklılık gösterebilir. Şimdi Web.config dosyasının içerisiğini görelim.
XML(Web.config)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5"/> <httpRuntime targetFramework="4.5"/> </system.web> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/> </compilers> </system.codedom> </configuration> |
Üstte her ASP.NET projesi oluşturulduğunda varsayılan olarak oluşturulan ama içeriği daha öncede bahsettiğimiz gibi Template türüne göre değişebilen Web.config adlı dosyayı görmektesiniz.
Web Transformlar
Web.Debug.config adlı dosya içerisinde transform işlemi olacak etiketlerin comment blokları arasına alındığını görüyoruz comment blokları arasına alınan 2 transform işlemi var biri connectionStrings etiketi bir diğeri ise customErrors etiketidir peki bu transformlar ne yapıyor ve nasıl transform işlemini yaptığından bahsedelim.
connectionStrings etiketi altında yer alan add etiketinde bir connectionString tanımı yer alıyor normal Web.config‘de yer alan connectionString tanımı haricinde 2 farklı attribute olduğunu görüyoruz bunlar xdt:Transform ve xdt:Locator attribute’leridir bunlar işlevi şudur.
xdt:Tranform: Yapacağınız işlemin ne olduğunu belirtiyoruz. Örnek SetAttributes ile xdt:Locator değerinde yer alan Match(name) tanımı sayesinde name=”MyDB” adlı bir connectionString tanımı yer alıyorsa Web.config dosyası içerisinde o zaman ilgili attibute’lerdeki değerler ile değiştiriliyor. Böylelikle Debug aşamasında farklı bir connection ve Release aşamasında farklı bir connection kullanabiliyor olacağız.
XML(Web.Debug.config)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="utf-8"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <!-- In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an attribute "name" that has a value of "MyDB". <connectionStrings> <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> --> <system.web> <!-- In the example below, the "Replace" transform will replace the entire <customErrors> section of your web.config file. Note that because there is only one customErrors section under the <system.web> node, there is no need to use the "xdt:Locator" attribute. <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors> --> </system.web> </configuration> |
Web.Debug.config dosyası içerisinden farklı olarak comment edilmemiş bir etiket var ve bu etiket sayesinde compilation etiketindeki debug attribute’ü xdt:Transform=”RemoveAttributes(debug)” tanımı sayesinde silinmektedir. Şimdide Web.Release.config adlı dosya içeriğine gözatalım.
XML(Web.Release.config)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="utf-8"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <!-- In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an attribute "name" that has a value of "MyDB". <connectionStrings> <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> </connectionStrings> --> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> <!-- In the example below, the "Replace" transform will replace the entire <customErrors> section of your web.config file. Note that because there is only one customErrors section under the <system.web> node, there is no need to use the "xdt:Locator" attribute. <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> <error statusCode="500" redirect="InternalError.htm"/> </customErrors> --> </system.web> </configuration> |
Makale girişindede bahsettiğimiz gibi Web Transform işlemi publish sırasında devreye giriyor. İşte bize bu makale başlığında belirttiğimiz gibi lazım olan şey build esnasında web transformun devreye girmesini bunun için ilk düzenlememizi şöyle yapıyoruz .csproj uzantılı dosyamızı bir editör yardımı ile açıyoruz ve </Project> etiketinden önce alttaki kodu eklemesiniz. Anlatacaklarım buraya kadar eğer bir eksiklik var ise yorum kısmından ulaşmaya çalışınız.
XML(ProjeAdi.csproj)
1 2 3 4 | <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\WebApplications\Microsoft.WebApplication.targets" /> <Target Name="BeforeBuild"> <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" /> </Target> |
[fa class=”fa-github fa-2x”] Bu makalede anlatılanlara ait projeyi Github’dan edinmek için buraya tıklayın.