为什么 ASP.Net 重写 runat=server 锚点控件的相对路径?

发布于 2024-08-26 05:45:26 字数 1091 浏览 2 评论 0 原文

我的 UserControls 位于我的解决方案中的 ~/Controls 文件夹中:

/Controls/TheControl.ascx

如果指定以下内容:

<a id="theId" runat="server" href="./?pg=1">link text</a>

ASP.Net 似乎想要重写路径以指向绝对位置。例如,如果控件位于 site.com/products/fish/cans.aspx 上,则链接 href 将被重写为“

<a id="munged_theId" href="../../Controls/?pg=1>link text</a>

为什么 Asp.Net 重写这些控件路径,是否有一种优雅的方法来修复它?”

我只是想让锚控件准确地说出我告诉它的内容!有那么难吗?

编辑:

我基本上已经按照凯尔西的建议做了。我知道我可以这样做,但当我想要相对简单的东西时,我不喜欢在代码中添加标记。至少它解决了问题:

Aspx 页面:

<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>

代码隐藏:

var anchor = new HtmlGenericControl("a") { InnerText = "Previous" + " " + PageSize) };
anchor.Attributes["href"] = "?pg=" + (CurrentPage - 1);
anchor.Attributes["class"] = "prev button";
ph.Controls.Clear();
ph.Controls.Add(anchor);

正如您所看到的,本质上应该是一个简单且轻量级的锚点所需的代码量,这不是最佳解决方案。我知道我可以使用文字,但我认为这更干净,因为我添加了多个锚点。

不过,我很想知道为什么 ASP.Net 接管并尝试修复我的 URL。

I have my UserControls in a ~/Controls folder in my solution:

/Controls/TheControl.ascx

If specify the following:

<a id="theId" runat="server" href="./?pg=1">link text</a>

ASP.Net seems to want to rewrite the path to point to the absolute location. For example, If the control is on site.com/products/fish/cans.aspx the link href will be rewritten to read

<a id="munged_theId" href="../../Controls/?pg=1>link text</a>

Why does Asp.Net rewrite these control paths, and is there an elegant way to fix it?

I just want the anchor control to spit out exactly what I tell it to!!! Is that so hard?

EDIT:

I've basically done what Kelsey suggested. I knew I could do it this way, but I don't like adding markup in my code when I want something relatively simple. At least it solves the problem:

Aspx page:

<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>

Code-behind:

var anchor = new HtmlGenericControl("a") { InnerText = "Previous" + " " + PageSize) };
anchor.Attributes["href"] = "?pg=" + (CurrentPage - 1);
anchor.Attributes["class"] = "prev button";
ph.Controls.Clear();
ph.Controls.Add(anchor);

As you can see by the amount of code needed for what is essentially supposed to be be a simple and light-weight anchor, it's not the most optimal solution. I know I could use a Literal but I figured this was cleaner as I'm adding more than one anchor.

I would be interesting in knowing WHY ASP.Net takes over and tries to fix my URL, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

書生途 2024-09-02 05:45:26

为什么你有 runat="server" 而没有定义 ID ?您需要在服务器端访问它吗?如果删除 runat="server" 一切都会按预期工作。

有关 ASP.NET 如何处理路径的详细信息,请查看这篇 MSDN 文章

编辑:您可以通过使用 Literal 控件然后向其输出原始 来解决该问题。

例如:

<asp:Literal ID="myLiteral" runat="server" />

myLiteral.Text = "<a href=\"./?pg=1\">link text</a>";

然后您可以根据需要在 Literal 上设置可见属性。

Why do you have runat="server" and no ID defined? Do you need to access it server side? If you remove the runat="server" everything will work as expected.

For more information regardinging how ASP.NET handles paths check out this MSDN article.

Edit: You can get around the problem then by using a Literal control and then outputing the raw <a href... to it.

Eg:

<asp:Literal ID="myLiteral" runat="server" />

myLiteral.Text = "<a href=\"./?pg=1\">link text</a>";

Then you can set the visible property on the Literal however you want.

初见 2024-09-02 05:45:26

我知道这是一个有点老的话题,但我也遇到了这个问题,最后采用了类似的解决方案,但通过在 ascx 中执行此操作能够节省几行代码:

<anchor id="myAnchor" runat="server" href="xxx">link text</anchor>

然后在在后面的代码中,我使用 HtmlGenericControl 引用了它,然后可以执行以下操作:

myAnchor.TagName = "a";
// other properties set as needed

无论如何,我想我应该发布以防其他人在这里遇到同样的问题。

I know this is a bit of an old topic, but I was running into this problem as well and in the end went with a similar solution, but was able to save a few lines of code by doing this in the ascx:

<anchor id="myAnchor" runat="server" href="xxx">link text</anchor>

Then in the code behind, I referenced it using an HtmlGenericControl and can then do this:

myAnchor.TagName = "a";
// other properties set as needed

Anyway, I thought I'd post in case anyone else stumbles in here with the same issue.

烦人精 2024-09-02 05:45:26

最好的办法是使用神奇的 ~/ 引入 url 来使所有应用程序根目录相对。这往往会让事情保持直线。

Best bet is to make everything app root relative using the magic ~/ lead-in to the url. That tends to keep stuff straight.

我也只是我 2024-09-02 05:45:26

你的问题没有一个很好的答案。 ASP.NET 将把 UserControl 中的相对路径视为相对于用户控件的路径。

您可以做的是在用户控件的后面代码中,根据 Request.Path 属性设置锚标记的 HRef 属性。然后您可以创建与该页面相关的 URL。

另一种方法是使用像 Kelsey 建议的文字,或者我会尝试将所有与 ~/ 相关的应用程序映射起来,就像 Wyatt 建议的那样。

There isn't a great answer to your question. ASP.NET is going to treat a relative path in a UserControl as relative to the path of the user control.

What you can do is in the code behind for your user control, set the HRef property of your anchor tag based on the Request.Path property. Then you can create URLs relative to the page.

Alternative is to use a literal like Kelsey was suggestion, or I would just try and map everything app relative with ~/ like Wyatt suggested.

牛↙奶布丁 2024-09-02 05:45:26

即使是文字也至少不能使用 ICallBackEventHandler 和 RenderControl 工作...我最终在客户端破解了标签:/例如在 JQuery 中:

$('#munged_theId').attr('href', './?pg=1');

Even a literal doesn't work using ICallBackEventHandler and RenderControl at least... I ended up hacking the tag back client-side :/ e.g in JQuery:

$('#munged_theId').attr('href', './?pg=1');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文