Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 动态构建URL,但去掉端口号或确保HTTP而不是HTTPS_Asp.net_Url_Replace_Port - Fatal编程技术网

Asp.net 动态构建URL,但去掉端口号或确保HTTP而不是HTTPS

Asp.net 动态构建URL,但去掉端口号或确保HTTP而不是HTTPS,asp.net,url,replace,port,Asp.net,Url,Replace,Port,当从HTTPS页面单击导航栏中的链接时,使用BuildAbsolute创建URL的my Link函数不起作用,因为URL在创建的URL中包含端口443。根据我的研究,似乎有几种方法可以去除端口号,我正在尝试使用uri.Host并手动构建URL 但是在代码行上,它说-Uri=newuri(string) 我得到无效的表达式术语“string” 这是我的Link.cs文件中的原始函数 public static string ToCategory(string departmentId, strin

当从HTTPS页面单击导航栏中的链接时,使用BuildAbsolute创建URL的my Link函数不起作用,因为URL在创建的URL中包含端口443。根据我的研究,似乎有几种方法可以去除端口号,我正在尝试使用uri.Host并手动构建URL

但是在代码行上,它说-
Uri=newuri(string)

我得到无效的表达式术语“string”

这是我的Link.cs文件中的原始函数

public static string ToCategory(string departmentId, string categoryId, string page)
    {
        DepartmentDetails d = CatalogAccess.GetDepartmentDetails(departmentId);
        string deptUrlName = PrepareUrlText(d.Name);
        CategoryDetails c = CatalogAccess.GetCategoryDetails(categoryId);
        string catUrlName = PrepareUrlText(c.Name);
        if (page == "1")
            return BuildAbsolute(String.Format("{0}-d{1}/{2}-c{3}", deptUrlName, departmentId, catUrlName, categoryId));
        else
            return BuildAbsolute(String.Format("{0}-d{1}/{2}-c{3}/Page-{4}/", deptUrlName, departmentId, catUrlName, categoryId, page));
}
这是我的用户控件中调用link函数的代码

  <ul>
     <asp:Datalist ID="deptList4" runat="server">
          <ItemTemplate>
              <asp:HyperLink ID="navCatList4" runat="server"
                   NavigateUrl= '<%# Link.ToCategory("4", Eval("CategoryID").ToString()) %>'
                   Text='<%# HttpUtility.HtmlEncode(Eval("Name").ToString()) %>'>
               </asp:HyperLink><br />
           </ItemTemplate>
      </asp:Datalist>
 </ul>
我的目标是构建url,但要确保如果从https页面单击链接,则不会使用443端口或https构建链接,因为这将导致页面无法加载

谢谢你的帮助

谢谢

替换当天保存的函数,并根据解决问题的建议将上面的代码调整为下面的代码

return BuildAbsolute(String.Format("{0}-d{1}/{2}-c{3}", deptUrlName, departmentId, 
    catUrlName, categoryId)).Replace(":443", "").Replace("https://", "http://");

如果您的URL字符串可能包含一些不需要的部分,则可以执行以下操作:

var sanitizedUrl = urlString.Replace(":443","").Replace("https://","http://");

您正在调用函数BuildAbsolute(string)来呈现绝对URL。这个函数很可能从被请求的页面获取URL端口和协议,并使用这些信息来构建URL

在https页面或路径中有端口的页面上,这将添加到返回的新路径中


您需要更改您需要使用一个新函数,该函数从请求的基本URL中删除协议和端口,然后创建您的URL

这非常清楚地解释了我的问题,但没有提供任何关于如何解决问题的细节。也许这是真的,但您没有提供BuildAbsolute,我只是建议这就是问题所在。不是在你发布的代码中。这对我很有效,我只是在我的BuildAbsolute行中添加并使用了以.Replace开头的代码。谢谢。这只有在https和http都使用标准端口时才有效。这可能并不总是如此。我同意,这不是最可靠的解决方案。然而,这个问题特别提到了“https”和“443”,所以这似乎是实现他们想要的最简单的方法。
var sanitizedUrl = urlString.Replace(":443","").Replace("https://","http://");