Tutorial :ASP.Net - Path Problems caused by Encoded URLs



Question:

I am building a web store and I am having a bit of a problem with some of the urls.

I have a large directory of departments and categories in my database that I am using to form my main menu.

The URLs have the form Application/Store/Department/Category (All store URLs are rewritten to store.aspx?Querystringstuff)

The problem is that some of the urls generated by my code work, but prevent the page's CSS from loading properly.

It is specifically the URLs who's source text contain slashes. I am URL encoding the source text of course but I'm still having the problem.

My css is linked in a master page-

 <link rel="stylesheet" type="text/css" href="~/CSS/Layout.css" runat="server">  

Example Links that work -

Ice Machines

http://localhost:1079/ABCWeb/Store/Refrigeration+Equipment/Ice+Machines.aspx

Steam Table Pans

http://localhost:1079/ABCWeb/Store/Catering+%26+Buffet/Steam+Table+Pans.aspx

Example Links that break-

Napkin Holders/Dispensers

http://localhost:1079/ABCWeb/Store/Tabletop/Napkin+Holders%2fDispensers.aspx

Salamanders/Cheesemelters

http://localhost:1079/ABCWeb/Store/Cooking+Equipment/Salamanders%2fCheesemelters.aspx

If it matters here is my code for building URLs- The array contains an array of items in the path The first element is not encoded as it contains the the current store path.

 private static System.Text.StringBuilder AppendAnchor(this System.Text.StringBuilder str, string[] path)  {         return str.Append("<a href='")          .Append(path.Aggregate((a, b) => a + "/" + HttpUtility.UrlEncode(b)))           .Append(".aspx'><span>")          .Append(HttpUtility.HtmlEncode(path[path.Length-1]))          .Append("</span></a>");  }  

Thanks for the help!


Solution:1

Try including your style-sheets using a path relative to the web root. An easy way to do this in ASP .NET webforms is to use ~ on a server-side control eg.

<link rel="Stylesheet" type="text/css" href="~/Css/MyCssFile.css" runat="server" />  

ASP.NET should then resolve the correct URL for you, regardless of what the URL is.


Solution:2

Well I looked a little further into it-- The "~" actually seems to be where the problem is occurring.

The links are working correctly but I think the server isn't parsing it correctly when it generates the relative paths...

Here is the css link from the generated code on a working link

../../CSS/Layout.css

Here is the css link on a broken page at the same depth

../../../CSS/Layout.css"

Here is a very ugly fix that works- not sure if there is a better way though

<link rel="stylesheet" type="text/css" href="<%=Request.ApplicationPath+"/Css/Layout.css" %>" />  

The strange thing is that the navigation links generated from similar paths in the sitemap are working fine. Maybe it understands the full hierchy better when it is generating paths from the site-map and doesn't try to build a relative path.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »