ASP.Net - Path Problems caused by Encoded URLs

谁说我不能喝 提交于 2019-12-13 03:09:25

问题


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!


回答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.




回答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.



来源:https://stackoverflow.com/questions/1194900/asp-net-path-problems-caused-by-encoded-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!