ASP.NET中的相对路径

Tom*_*len 12 asp.net path relative-path tilde

<head runat="server">
    <meta charset="UTF-8" />
    <title>Make Games with Scirra Software</title>
    <meta name="description" content="Game making with Construct." />
    <meta name="keywords" content="game maker, game builder, html5, create games, games creator" />
    <link rel="stylesheet" href="~/css/default.css" />
    <link rel="stylesheet" href="~/plugins/coin-slider/coin-slider-styles.css" />
    <link rel="shortcut icon" href="~/images/favicon.ico" />
    <link rel="apple-touch-icon" href="~/images/favicon_apple.png" />
    <script src="~/js/googleAnalytics.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)

呈现为:

<head>
    <meta charset="UTF-8" />
    <title>Make Games with Scirra Software</title>
    <meta name="description" content="Game making with Construct." />
    <meta name="keywords" content="game maker, game builder, html5, create games, games creator" />
    <link rel="stylesheet" href="../css/default.css" />
    <link rel="stylesheet" href="../plugins/coin-slider/coin-slider-styles.css" />
    <link rel="shortcut icon" href="../images/favicon.ico" />
    <link rel="apple-touch-icon" href="../images/favicon_apple.png" />
    <script src="~/js/googleAnalytics.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)

为什么是JavaScript URL ~/而不是../

dan*_*ana 22

实际上是奇怪的实现,但不幸的是,这是ASP.NET处理这个问题的方式.这是我要做的补偿:

<script src="<%=ResolveClientUrl("~/js/googleAnalytics.js")%>"></script>
Run Code Online (Sandbox Code Playgroud)

  • 您可能只需要ResolveUrl(),因为ResolveClientUrl创建整个URL,而不仅仅是相对路径. (5认同)
  • Url.Content("〜/ js/googleAnalytics.js")适用于MVC (2认同)