views:

190

answers:

4

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.

+1  A: 

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

Wyatt Barnett
That doesn't solve the problem. This User Control can be used on multiple pages.
Atømix
Actually, it does. Relative url references resolve to correct approot-relative url. All the time. Like clockwork. And the Caps.
Wyatt Barnett
+4  A: 

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.

Kelsey
Yes. I'm controlling its visible property. I left it out for simplicity's sake and because the id will be munged on the client. I'll add one in.
Atømix
Thanks, this is basically what I ended up doing. I have a PlaceHolder and generate a HtmlGenericControl("a")
Atømix
A: 

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.

Andrew Barrett
It's annnnnoying. Actually, I tried generating an HtmlAnchor in Code-Behind and it does the same as <a runat=server>
Atømix
+1  A: 

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.

Eric
Very interesting solution!
Atømix