由编码的URL引起的路径问题

由编码的URL引起的路径问题

本文介绍了ASP.Net-由编码的URL引起的路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个网上商店,而某些网址有些问题.

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.

URL的格式为应用程序/商店/部门/类别"(所有商店URL都重写为store.aspx?Querystringstuff)

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

问题是我的代码生成的某些url有效,但是阻止了页面CSS的正确加载.

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

特别是URL的源文本中包含斜杠.当然,我正在对源文本进行URL编码,但是仍然存在问题.

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.

我的CSS链接在母版页中-

My css is linked in a master page-

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

有效的示例链接-

制冰机

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

蒸汽桌锅

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

断开链接的示例-

餐巾架/分配器

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

萨拉曼德斯/奶酪制造商

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 pathThe 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>");
}

感谢您的帮助!

推荐答案

好吧,我进一步看了一下-实际上,〜"似乎是问题所在.

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

这是工作链接上生成的代码中的css链接

../../CSS/Layout.css

以下是残破页面上具有相同深度的css链接

../../../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.

这篇关于ASP.Net-由编码的URL引起的路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:48