6

【茶包射手日記】ASP.NET網站bindingRedirect無效

 1 year ago
source link: https://blog.darkthread.net/blog/bindingredirect-failed/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【茶包射手日記】ASP.NET網站bindingRedirect無效-黑暗執行緒

故事從某個Windows 2003上的ASP.NET 3.5網站搬到Windows 2012 R2說起,移至新主機後蹦出以下訊息:

Could not load file or assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

這問題可難不倒熟悉ASP.NET歷史的老骨頭,System.Web.Extensions 1.0.6來自AJAX Control Toolkit,是ASP.NET 2.0時代要實現AJAX功能的套件,到ASP.NET 3.5時納為標準配備不需另外安裝,但版本升級為3.5。這個網站從ASP.NET 2.0一路開發,後來才升級到3.5,AJAX套件一直運作良好沒理由調整,直到搬遷新主機少了AJAX Control Toolkit才發生問題。

要解決問題有兩個方法:在Windows 2012 R2安裝AJAX Control Toolkit(這… 太Low了,拎杯下不了手),或是透過bindingRedirect將1.0.6繫結重新導向3.5版。

排版顯示純文字
<runtime>
    <assemblyBinding appliesTo="v2.0.50727">
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
 </runtime>

熟門熟路調好設定準備手到擒來… 噹!踢到鐵板,錯誤訊息依舊。

反覆檢查config設定,張大眼睛撐到眼眶滲血也看不出異常。不得已使出大絕-愚公移山法,找到另一個類似背景但在該主機上可以正常運作的web.config逐行比對,將可疑之處一行一行調到跟可運作版一致。最後,關鍵竟在想也想不到的地方:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

將xmlns移除,問題就消失了!

找到關鍵後爬文,發現這是曾讓不少人摔坑的經典問題(參考 1 2 3),好發於舊版ASP.NET網站,我實測用VS2013/VS2015建立ASP.NET 4網站,<configuration>已無任何Namespace宣告。至於為什麼加上xmlns會導致bindingRedirect失效?爬完文仍未得解,留下結論:

遇到bindingRedirect設定無效時,請檢查是否<configuration>是否多了xmlns設定。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK