ASP.NET相对路径 [英] ASP.NET relative path

查看:124
本文介绍了ASP.NET相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑与ASP.NET相对路径,请有人可以帮忙吗?

I'm confused with ASP.NET relative path, please can someone help?

在母版页我给了一个链接标签参考:

In a Master Page I gave a link label referencing:

<a href="~/Account/Login.aspx">Login</a>

从ASP.NET官方文档我读

From the ASP.NET official documentation I read:

下面的例子显示使用使用Image服务器控件在这个例子中,当指定图像的根目录相对路径在〜运算,图像文件从位于图像文件夹读直属Web应用程序的根目录,无论在哪里在网站页面的位置。

The following example shows the ~ operator used to specify a root-relative path for an image when using the Image server control In this example, the image file is read from the Images folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located.

&LT; ASP:图像=服务器ID =Image1的
  的ImageUrl =〜/图片/ SampleImage.jpg/&GT;

通过登录标记,当我点击从/帐户文件夹页面的链接,我重定向到:

With the Login markup, when I click the link from a page in the /Account folder, I'm redirected to:

/Account/~/Account/Login.aspx

为什么呢?为什么2 H

Why? WHY?h

推荐答案

由于你在标记直接使用它,而不是在服务器控件。事情如此简单应该修复它:

Because you're using it directly in markup, rather than in a server control. Something as simple as this should fix it:

<a runat="server" href="~/Account/Login.aspx">Login</a>

基本上,路径参考需要在服务器上进行转换,因为它是应用程序的基本目录的服务器路径的参考。纯HTML标记不处理在服务器上,它只是交付,是给客户端。只有服务器处理code将路径转换为它解析为。

Basically, the ~ path reference needs to be translated on the server, since it's a reference to the server path of the application's base directory. Plain HTML markup isn't processed on the server, it's just delivered as-is to the client. Only server-processed code will translate the ~ path to what it resolves to.

这篇关于ASP.NET相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆