Html 如何在aspx页面中为href生成url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22097609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:05:00  来源:igfitidea点击:

How to generate url for href in aspx page

htmlasp.netwebforms

提问by flying227

How does one generate a URLin a master page's ASPXpage so that the reference works everywhere across the app?

如何URL在母版页的ASPX页面中生成 a以便引用在整个应用程序中随处可见?

Here is an example from a menu in a master page:

以下是母版页中菜单的示例:

<a href="Admin/AddVendor.aspx">Add Vendor</a>

The problem is that if I leave the URLas it is on the master page, then when someone is already on a page that resides in the Admin folder, this URLno longer works. I need to path the URLfrom the root every time, but forget how to do this in WebForms.

问题是,如果我URL在母版页上保持原样,那么当有人已经在驻留在 Admin 文件夹中的页面上时,这URL将不再起作用。我URL每次都需要从根开始路径,但忘记如何在WebForms.

回答by JW Lim

Try using this, which maps to your website's root directory:

尝试使用它,它映射到您网站的根目录:

<a href="/Admin/AddVendor.aspx">Add Vendor</a>

EDIT: In that case, you could do this:

编辑:在这种情况下,你可以这样做:

<a href="<%= Page.ResolveUrl("~/Admin/AddVendor.aspx") %>">Add Vendor</a>

It would be far easier to just use the asp.net Hyperlink control though

虽然只使用 asp.net 超链接控件会容易得多

<asp:HyperLink ID="Link1" runat="server" NavigateUrl="~/Admin/AddVendor.aspx" Text="Add Vendor" />

回答by Mason

runat server html control could map site directory

runat 服务器 html 控件可以映射站点目录

<a runat="server" href="~/Admin/AddVendor.aspx">Add Vendor</a>