Ayorus Ayorus - 4 months ago 17
ASP.NET (C#) Question

IIS changes domian in href attribute of a(link) tags

I have the following code in one aspx file:

<a href="<%# myfunction() %>" ...


myfunction returns a very simple string like this:

return "http://domain.com/link";


Locally the code runs smoothly, however, when I migrate it to the production environment. All the href attributes are changed to:
http://serverdomain.com/link


This is very strange to me, I have done this several times before. I do not know what I am doing wrong this time.

Does any one have any idea about what I am missing?

Answer

IIS isn't causing any effect here, the behavior is defined by the HTML spec. For navigation links, there are four common conventions an href attribute can follow:

  1. Beginning with http:// or //. The browser will resolve the link as an absolute URL.

  2. Beginning with a single /. The browser will resolve the link as relative to the domain.

  3. Beginning with text. The browser will resolve the link as relative to the page.

  4. Beginning with #. The browser will look for an HTML element on the same page (by ID) and scroll to it if found.

I would compare the strings of the return value of your function, and the actual HTML markup rendered (not just what is navigated to or shown on hover).

To use an example on this page:

<a href="/questions">Questions</a> is a relative URL, which resolves to http://stackoverflow.com/questions.

<a href="http://example.com/questions">Questions</a> is an absolute URL, and resolves to an external domain at http://example.com/questions.

Per one of your comments, the value your function would create a relative URL. If <a href="domain.com/link?param="> was on this page, it would resolve http://stackoverflow.com/ as the domain.