天天看點

自定義aspnet_client的位置

  有人發帖問“aspnet_client必須放在web根目錄嗎?怎樣改變這個路徑?”

  我一開始猜想這個路徑可能是寫死的。為了證明一下,就打開了Reflector,試圖尋找到類似"/aspnet_client/system_web/1_1_4322/"的字元串,卻發現這個目錄的位置是可以自定義的,但是MSDN中沒有公開。相關代碼在System.Web.UI.Util.GetScriptLocation(HttpContext)中,

 1

自定義aspnet_client的位置

internal static string GetScriptLocation(HttpContext context)

2

自定義aspnet_client的位置
自定義aspnet_client的位置

...{

3

自定義aspnet_client的位置

      string text1 = null;

4

自定義aspnet_client的位置

      IDictionary dictionary1 = (IDictionary) context.GetConfig("system.web/webControls");

5

自定義aspnet_client的位置

      if (dictionary1 != null)

6

自定義aspnet_client的位置
自定義aspnet_client的位置

      ...{

7

自定義aspnet_client的位置

            text1 = (string) dictionary1["clientScriptsLocation"];

8

自定義aspnet_client的位置

      }

9

自定義aspnet_client的位置

      if (text1 == null)

10

自定義aspnet_client的位置
自定義aspnet_client的位置

      ...{

11

自定義aspnet_client的位置

            throw new HttpException(HttpRuntime.FormatResourceString("Missing_clientScriptsLocation"));

12

自定義aspnet_client的位置

      }

13

自定義aspnet_client的位置

      if (text1.IndexOf("{0}") >= 0)

14

自定義aspnet_client的位置
自定義aspnet_client的位置

      ...{

15

自定義aspnet_client的位置

            string text2 = "system_web";

16

自定義aspnet_client的位置

            string text3 = VersionInfo.IsapiVersion.Substring(0, VersionInfo.IsapiVersion.LastIndexOf('.')).Replace('.', '_');

17

自定義aspnet_client的位置

            text1 = string.Format(text1, text2, text3);

18

自定義aspnet_client的位置

      }

19

自定義aspnet_client的位置

      return text1;

20

自定義aspnet_client的位置

}

  然後再machine.config中找到了預設的配置:

  <webControls clientScriptsLocation="/aspnet_client/{0}/{1}/" />

  當然也可以在web.config中針對單個應用程式進行配置。如果要把aspnet_client目錄放在根目錄下的myweb中,那麼就在web.config檔案的

<configuration> 

    <system.web>

标簽内加上這麼一句:

    <webControls clientScriptsLocation="/myweb/aspnet_client/{0}/{1}/" />

  或者直接寫

    <webControls clientScriptsLocation="/myweb/aspnet_client/system_web/1_1_4322/" />

  就可以了。