开发者

.net - how to fix my urls?

I have a footer control with the following contents

<img src="images/img1.gif" />
<img src="images/img1.gif" />

Then I have two pages that include it like so

//Default.aspx
<%@ Register Src="~/Controls/Footer.a开发者_JAVA技巧scx" TagPrefix="his" TagName="SiteFooter"%>
<his:SiteFooter ID="SiteFooter" runat="server" />

//electronics/Default.aspx
<%@ Register Src="~/Controls/Footer.ascx" TagPrefix="his" TagName="SiteFooter"%>
<his:SiteFooter ID="SiteFooter" runat="server" />

When I view the page in electronics/Default.aspx, the images are broken because the images directory is one directory up. This project is being served from a virtual path, so web pages will appear with urls such as

http://mysite.com/virtualpath/Default.aspx
http://mysite.com/virtualpath/electronics/Default.aspx

In PHP, I would prefix those footer images with <?=PROJECT_URL ?>. What is the equivalent approach to this in .net? Or is there a better way to fix these broken relative paths depending on where you include from?


http://msdn.microsoft.com/en-us/library/ms178116.aspx:

<asp:Image runat="server" id="Image1" ImageUrl="~/images/img1.gif" />
<asp:Image runat="server" id="Image2" ImageUrl="~/images/img2.gif" />

or

<img src="./images/img1.gif" />
<img src="./images/img2.gif" />

or

<img src="/images/img1.gif" />
<img src="/images/img2.gif" />

I found one of the above to always work.


Making the img a aspx control (add runat="server") should allow you to use ~ - try this -

<img runat="server" src="~/images/img1.gif" />
<img runat="server" src="~/images/img1.gif" />

See URLs in Master Pages for details


The quick fix is to use the .. to go up one dir before going down into the images folder:

<img src="../images/img1.gif" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜