ã¯ããã«
Visual Studioã§ãããžã§ã¯ããéçºããã³ãããã°ãããšãã¯ãéåžžããããã°æ§æã§ã³ãŒããã³ã³ãã€ã«ããŠãããè¡ããŸãã ãããžã§ã¯ãã¯ããã«ãµãŒããŒã«é 眮ããããªãªãŒã¹æ§æã§ã³ã³ãã€ã«ãããŸãã ãããŠãåãããžã§ã¯ãã¯é ããæ©ãã倧ãããªããäžçš®ã®ããããã°ããŸãã¯ããªãªãŒã¹ãã³ãŒããã€ãŸããããããã®æ§æã§ã®ã¿åäœããã³ãŒãã«ãªããŸãã åé¡ãã³ãŒãã®ã¿ã«éå®ãããŠããå ŽåãïŒifæ¡ä»¶ä»ãã³ã³ãã€ã«ãã£ã¬ã¯ãã£ãã¯ãã¹ãŠã®åé¡ã解決ããŸãã ããããéåžžãåæ§æã«åºæã®ã³ãŒããããã³å€ãã®å ŽåWebããŒãžã®ã¬ã€ã¢ãŠãããã³ãŒãã®èåŸã«åºãã£ãŠããŸãã åé ã§ããã®ãããªåé¡ãçŸãå§ããŠããããŸã æ·±å»ãªé çã§ã¯ãªãå Žåãéåžžã®ã³ã¡ã³ãã¯éåžžãç¹å®ã®ç¬éã«å¿ èŠã§ã¯ãªãè¡/ã³ãŒã/ããŒã¯ã¢ãã/èšå®ã«ã³ã¡ã³ãããããã«äœ¿çšãããŸãã ããããããã¯æãçŽç²ãªåœ¢ã®ãå°çãžã®éãã§ãã ãããŠãããã§å€©åœãžã®é段ã«ã€ããŠã話ããããšæããŸãã
ç§ããã®ãããªåé¡ã«æåã«ééããã³ã¡ã³ããããŸããŸãªæ§æãã¡ã€ã«ã®ã»ãããããã§ã«æŸèæã«å€¢äžã«ãªã£ããšããããæç¹ã§ä»ã®äººã®çµéšã«èå³ãæã¡ãéçºç°å¢ãšäœ¿çšãããããŒã«ã®å¯èœæ§ãããããæ¢ãããšã«æ±ºããŸããã ãããŠãçªç¶ããã¹ãŠã®äººå·¥æŸèæãå®å šã«åé·ã§ããããšãããããŸãããåé¡ã¯æšæºçãªæ段ã§ãã³ãŒãã®å¯èªæ§ãæãªãããšãªã解決ã§ããŸãã ç 究ã®çµæãçãèšäºã§èª¬æããŸããããã§ã¯ãç°å¢ã®åé¡ã®äŸãšããããžã§ã¯ããå ¬éãããšãã«ãã¹ãŠã®ã³ãŒããèšå®ãããã³htmlããŒã¯ã¢ãããç®çã®åœ¢åŒã«èªåçã«å€æŽã§ããã¡ã«ããºã ãæ€èšããŸãã ããã¯ãç§ãäž»ã«ä»äºãããŠããåéãšããŠãASP.NET MVCã¢ããªã±ãŒã·ã§ã³ã®éçºã«çŠç¹ãåœãŠãŸãã ãã ããæžããããã®ã¯ãã¹ãŠãVisual Studioã§ã®.NETéçºã«é©çšã§ããŸãã
Web.configãã¡ã€ã«ã®å€æ
ãã®èšäºã§ã¯ãæåã®éèŠãªããŒã«ã§ããweb.configãã¡ã€ã«ã®å€æããå§ããŸãã ããã§ããã®ææ³ã«æ £ããŠããªãå Žåã¯ãå¿ ããã®ã»ã¯ã·ã§ã³ããèªã¿ãã ããã
ãã®å€æã¯Visual Studio 2010ã§ç»å ŽããDEVç°å¢ãšPRODç°å¢ã®ããŸããŸãªèšå®ã«é¢é£ããèšå€§ãªæ°ã®åé¡ãããã«è§£æ±ºããŸããã åæã«ãå€æãå®è¡ããã«ãŒã«ã¯éåžžã«ã·ã³ãã«ã§æè»ã§ãã
VS 2010以éãweb.configãã¡ã€ã«ãçªç¶ãããžã§ã¯ãããªãŒã«ã³ã³ãããšããŠè¡šç€ºããå§ãããã®äžã«2ã€ã®æ°ããweb.release.configãã¡ã€ã«ãšweb.debug.configãã¡ã€ã«ãé 眮ãããŠããããšã«æ°ã¥ããã§ãããã ãããã¯ãã¡ã€ã³web.configãã¡ã€ã«ã®å€æãæ§æãããŠãããã¡ã€ã«ã§ãã ãããã®2ã€ã®ãã¡ã€ã«ã¯ãŸã£ãã䜿çšã§ãããã¡ã€ã³ã®web.configãå¯äžã®ãã¡ã€ã«ãšããŠäœ¿çšã§ããŸããããã«ãããåé¡ãçããããšã¯ãããŸããã ãããããã®çŠ ãç解ãããã®åŸãåžžã«è³¢æã«å®è·µããããã«ã¯ããããããè¯ãã§ãããã
å€æãã©ã®ããã«æ©èœããããç 究ããäžã§ãæãéèŠãªããšã¯ãèšå®æã«2ã€ã®äž»ãªããšãæ確ã«ç解ããããšã§ãã
1.å€æã¯ããããžã§ã¯ãã®å ¬éæã«ã®ã¿æ©èœããŸãã
2. web.debug.configããã³web.release.configãã¡ã€ã«ã¯å ã®web.configã眮ãæããŸãããã å€æŽïŒå€æïŒããŸã
æåã®æ®µèœã¯ãéçºã«ãããå€æã®äœ¿çšã«é倧ãªå¶éã課ããŠããããã«æãããŸãã ãããŠãå®éã«ã¯ãããšãã°IIS Expressã®äžã§ãã€ãŸããããªã±ãŒã·ã§ã³ãªãã§ãããžã§ã¯ããéçºããŠãããšããäœãè¯ãã§ããããïŒ ããããå®éã«ã¯ããã¯åé¡ã§ã¯ãããŸãããããã®ç¹ã«ã€ããŠã¯åŸã§è§ŠããŸãã
2çªç®ã®ç¹ã«ã€ããŠã¯ãããã¯ç§ãå«ãæåã®å€æã«ééãã人ã®ããããééãã§ãããŸããå ¬éæã«web.configãã¡ã€ã«ã察å¿ãããªãªãŒã¹/ãããã°ãã¡ã€ã«ã«çœ®ãæããããšã決ããŸããã ãã¡ãããæåã®åºçç©ã¯ããã¥ã¢ã«ã泚ææ·±ãèªã¿ãŸãããããã¯ãŸã£ããæ©èœããªãããã§ãã
ããã¯æ¬¡ã®ããã«æ©èœããŸããå€æãã¡ã€ã«ã§ã¯ãé©åãªæ§æã§å ¬éãããšãã«å ã®web.configãå€æŽããæ¹æ³ãæå®ãããŸãã ç§ãã¡ã¯é ãã«è¡ããªãã§ãæšæºçãªäŸãèããŸãã èªåçæãããweb.release.configãã¡ã€ã«ã«ã¯ãããã©ã«ãã§å€æã®ããã®1ã€ã®æšæºãã£ã¬ã¯ãã£ãããã§ã«å«ãŸããŠããŸãã ãªã¹ãã¯æ¬¡ã®ãšããã§ãïŒåé€ãããã³ã¡ã³ãä»ãïŒã
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration>
æåã«ãéèŠãªç¹ã«æ³šæããŠãã ãããXML-Document-Transformåå空éã®xdtãã¬ãã£ãã¯ã¹ã¯ãæ§æèŠçŽ ã§å®çŸ©ãããŸãã ãã®åå空éã§ã¯ãå€æãå®çŸ©ãããŠãã2ã€ã®å±æ§ãLocatorãšTransformã宣èšãããŠããŸãã Locatorå±æ§ã¯ãæ§æãã¡ã€ã«ã§äœãå€æŽããããæ£ç¢ºã«æ±ºå®ããTransformå±æ§ã¯ãããè¡ãæ¹æ³ã瀺ããŸã ã äžèšã®äŸã§ã¯ãTransformã®ã¿ã䜿çšãããŸãããã®å€ã¯ãå ã®web.configãã¡ã€ã«ã®ã³ã³ãã€ã«èŠçŽ ã®debugå±æ§ãåé€ããå¿ èŠãããããšã瀺ããŠããŸãïŒãã¡ããããã®å€ãšå ±ã«ïŒã
åèïŒ configuration / system.web / compilationèŠçŽ ã®debugå±æ§ã¯ãasp.netããŒãžã®ã³ã³ãã€ã«ãæ åœããŸãã ããã©ã«ãã®web.configãã¡ã€ã«ã§ã¯ããã®å±æ§ã®å€ã¯éåžžtrueã§ãããããã¯asp.netããŒãžããããã°æ å ±ãè¿œå ãããŠã³ã³ãã€ã«ãããããšãæå³ããŸãã ãªãªãŒã¹æ§æïŒã€ãŸããéåžžPRODã§ã®ã¬ã€ã¢ãŠãçšã«ãããžã§ã¯ããã³ã³ãã€ã«ãããæ§æïŒã§ã¯ãããã¯å¿ èŠãããŸããããã®ããããã®å±æ§ãåé€ããã³ãã³ãã¯ãããã©ã«ãã§web.release.configå€æãã¡ã€ã«ã«æ¿å ¥ãããŸãã ããã«ã€ããŠã¯ãåŸã§è©³ãã説æããŸãã
äŸã«æ»ããŸãããã ãããžã§ã¯ãããªãªãŒã¹æ§æã§å ¬éããããšãæ§æå€/ system.web / compilationèŠçŽ ã®ãããã°å±æ§ã¯ããã®å€ãšãã®å€ãååšãããã©ããã«é¢ä¿ãªããæçµçãªweb.configãã¡ã€ã«ããåé€ãããŸãã æ§æãã¡ã€ã«ã®ç¹å®ã®èŠçŽ ã«å¯ŸããŠå€æãæ瀺çã«èšå®ãããŠããããããã®äŸã§ã¯ãã±ãŒã¿ãŒå±æ§ãæ¬ èœããŠããããšã«æ³šæããŠãã ããã ãã ããLocatorå±æ§ã䜿çšãããšãç¹ã«XPathåŒã䜿çšããããå±æ§å€ã®æ瀺çãªå¯Ÿå¿ãèšå®ãããããŠãããæè»ãªæ¹æ³ã§å€æã®ããã«web.configãã¡ã€ã«ã®èŠçŽ ãæå®ã§ããŸãã å¥ã®äŸã次ã«ç€ºããŸãã
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="Setting1" value="SettingValue" xdt:Locator="Condition(@key='key1')" xdt:Transform="Replace"/> </appSettings> </configuration>
ãã®äŸã§ã¯ããœãŒã¹web.configã§ããŒkey1ã®èšå®ãæ€çŽ¢ããïŒããã¯ãã±ãŒã¿ãŒã®XPathã§ç€ºãããŸãïŒããããèŠã€ãã£ãå Žåã¯å®å šã«çœ®ãæããããŸã
ïŒTransformå±æ§ã®Replaceå±æ§ã®å€ïŒã䜿çšããŠãå€æã䜿çšããŠãã¡ã€ã«ããèšå®ããŸãã Locatorå±æ§ã®Conditionå€ã«ã¯ããããå®çŸ©ãããŠããæ§æãã¡ã€ã«ã®èŠçŽ ã«é©çšãããXPathåŒãå«ãŸããŸãããèŠçŽ èªäœã¯èªåçã«XPathåŒã®çŸåšã®èŠçŽ ã«ãªããŸãã æ¡ä»¶ã«å ããŠãXPathå€ã¯Locatorå±æ§ã§æå®ã§ããŸããXPathåŒãã°ããŒãã«ã³ã³ããã¹ãã§èæ ®ãããç¹ã®ã¿ãç°ãªããŸãã
å¥ã®äŸïŒ
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.web> <customErrors mode="On" xdt:Transform="Insert"> <error statusCode="404" redirect="/Error404" /> <error statusCode="500" redirect="/Error500" /> </customErrors> </system.web> </configuration>
ãã®äŸã§ã¯ãcustomErrorsã®ã»ã¯ã·ã§ã³å šäœãæçµçãªweb.configã«è¿œå ããŸãïŒã¡ãªã¿ã«ããšã©ãŒãçºçããå Žåã®Webã¢ããªã±ãŒã·ã§ã³ã®åäœãæ§æããããã«éåžžã«äŸ¿å©ã§ãã䜿çšãããã»ã¯ã·ã§ã³ã§ãïŒã Transformå±æ§ã®Insertå€ã䜿çšãããšãå ã®web.configã«ãã®ã»ã¯ã·ã§ã³ãååšããªãããšãåæãšããŠããŸãã ååšããå Žåã¯ãInsertã§ã¯ãªãReplaceã䜿çšããå¿ èŠããããŸãã
ãã®çµæãããŸããŸãªæ¹æ³ã§ãããžã§ã¯ããå ¬éãããšãã«web.configãã¡ã€ã«ããªã³ã¶ãã©ã€ã§å€æŽã§ãã匷åãªããŒã«ããããŸããã€ãŸããåã ã®èŠçŽ ãŸãã¯èŠçŽ å±æ§ïŒããã³èŠçŽ ã®ã°ã«ãŒãå šäœïŒãå€æŽãåé€ããŸãã¯è¿œå ããå€ãå€æŽããŸãèŠçŽ ã®å±æ§ã ããã«ããããããã°äžã«web.configãã¡ã€ã«ã®èšå®ããå®æçãªããã¹ãã/ãæŠéãã®ä»£æ¿ã«é¢ããã³ã¡ã³ããåãé€ãããšãã§ããŸãã å€æå±æ§å€ã®å®å šãªãªã¹ããšäœ¿çšäŸã¯ã MSDNã§èŠã€ããããšãã§ããŸãã
ç§ã®ãããžã§ã¯ãã§ã®å€æã®å®éã®äœ¿çšã«ã€ããŠè©±ãå Žåãweb.configãã¡ã€ã«ã®ãã¹ãŠã®èšå®ã®ãã¡ã次ãæãé »ç¹ã«åœ±é¿ãåããŸãã
1.æ¥ç¶æååãã€ãŸããã¹ãŠã®ããŒã¿ããŒã¹æ¥ç¶èšå®ã ããã¯ãæ¥ç¶æååèªäœãšãã¹ãŠã®ORMãã¬ãŒã ã¯ãŒã¯ã®èšå®ã«ãé©çšãããŸãã
2. appSettingsã»ã¯ã·ã§ã³ã®èšå®ã ãã¹ãŠã®èšå®ã¯ãéçº/ãããã°ã¢ãŒãããæŠé䜿çšã¢ãŒãã«è»¢éãããŸãã
3. Webã¢ããªã±ãŒã·ã§ã³ã®ãšã©ãŒåŠçèšå®-äžèšã®äŸã®ã»ã¯ã·ã§ã³/<system.web>/ã ç§ã®ããã«å人çã«éçºããã³ãã¹ããããšãã¯ãããã¹ããšã¹ã¿ãã¯ãã¬ãŒã¹ãå«ããã¹ãŠã®ãšã©ãŒãäžåºŠã«ç¢ºèªããçŸããã¹ã¿ãããŒãžã«ã¹ããŒãããªãããšãæãŸããã§ãã ãã¡ãããããã¯æŠéã¢ããªã±ãŒã·ã§ã³ã«ã¯åãå ¥ããããŸããã
4.èšå®ãã¬ãŒã¹/ãã°/ãããã°ã äžèšã®èšå®ã«å ããŠã>> web.configããããããã°ãšãã¬ãŒã¹ã«é¢é£ãããã¹ãŠã®èšå®ãã¯ãªã¢ãããŸãïŒãŸãã¯æ®ãããŸããããªãã«ãªããŸãïŒã ãããŠããã°èšå®ã¯ãã©ãã€ãã¢ãŒãããåäœã¢ãŒãã«è»¢éãããŸãã
5. WebããŒãžãã£ãã·ã¥ãããã¡ã€ã«ã®èšå®ã ãããã°äžãããŒãžã®HTTPãã£ãã·ã¥ã¯å®å šã«äžèŠã§ãããããweb.debug.configã®ãã£ãã·ã¥ãããã¡ã€ã«ã¯ãenabled = = falseå±æ§ãä»ããŠåçŽã«ç¡å¹ã«ãããŸãã ïŒOutputCacheå±æ§ã§ãã£ãã·ã¥ãããã¡ã€ã«ã䜿çšããŸããïŒïŒ
ãã¡ãããããã¯å®å šãªãªã¹ãã§ã¯ãããŸãã-ãã¹ãŠã¯ç¹å®ã®ãããžã§ã¯ãã«äŸåããŸãã ããããã»ãšãã©ãã¹ãŠã®å Žåãå€æã«ãã£ãŠåœ±é¿ãåããã®ã¯ãŸãã«ãããã®èšå®ã§ãã
çºè¡ãçºçããªãIIS Expressã䜿çšããéçºã«ã€ããŠã¯ãåè¿°ããããã«ãããã¯å€æã䜿çšããå Žåã®åé¡ã§ã¯ãããŸããã ç§ã¯éåžžã«é »ç¹ã«IIS Expressã䜿çšããŠïŒã€ãŸããå®éã®WebãµãŒããŒã«å ¬éããã«ïŒWebã¢ããªã±ãŒã·ã§ã³ãéçºããŸããå ã®web.configãã¡ã€ã«ã«ã¯ãDEVããŒãžã§ã³ã§å¿ èŠãªãã¹ãŠã®èšå®ãå«ãŸããå€æã¯Webã§ã®ã¿èšå®ãããŸã.release.configã ã€ãŸããå ã®web.configã¯åºæ¬çã«web.debug.configãã¡ã€ã«ãšããŠæ©èœããŸãã
ãã®ãããVisual Studioã䜿çšãããšãããŸããŸãªæŸèæãã¹ã¿ãã䜿çšããŠã³ãŒããã¬ã€ã¢ãŠããèšå®ã«åã蟌ãããšãªããDEVããã³PRODç°å¢ã§ã¢ããªã±ãŒã·ã§ã³ãæè»ã«å¶åŸ¡ã§ããŸãã ããã§ããã®ææ³ãææããŠããªãå Žåã¯ããã®ã®ã£ãããæ©æ¥ã«è§£æ¶ããå¿ èŠããããŸãã 次ã®èšäºã§-çž®å°ããã³ãã«ãã¹ã¯ãªããããããã°ãããã³ãããæ£ãã䜿çšããæ¹æ³ã«ã€ããŠã