【问题标题】:How to remove backslash from url when returning from c# code?从 c# 代码返回时如何从 url 中删除反斜杠?
【发布时间】:2017-09-22 11:53:04
【问题描述】:
    public Uri GetCrmUrl(string phone,string organisationid,string server)
    {
        string _URL = string.Empty;
        string clienturl = @"https://petsolutions.crm8.dynamics.com/nga/engagementhub.aspx?org="+organisationid+"&server="+server;
        Uri x = new Uri(clienturl,UriKind.Absolute);
        return x;
    }

我正在尝试直接在 html 上返回此 url。 但是返回的值是这样的:

https:\/\/petsolutions.crm8.dynamics.com\/nga\/engagementhub.aspx?org=dkejverv&server=dfvfevf

而预期的输出是:

https://petsolutions.crm8.dynamics.com/nga/engagementhub.aspx?org=dkejverv&server=dfvfevf

如何从我的 URL 中删除这些斜杠?

在 C# 代码中,代码返回正确的值,但在 HTML 呈现时,那里有一些反斜杠。 Check Image Here

【问题讨论】:

    标签: c# html visual-studio web-services svc


    【解决方案1】:

    也许你应该试试 String.Replace() 方法:

    string test = clienturl.Replace(@"\", string.empty);
    Uri x = new Uri(test ,UriKind.Absolute);
        return x;
    

    【讨论】:

    • String.Replace 在 c#code 中运行良好。在 HTML 中返回值时出现问题
    【解决方案2】:

    我不知道你在哪里寻找你的字符串,但这个例子正是你想要的方式:

    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Uri test = GetCrmUrl("00000", "orgid", "server");
    
                Console.WriteLine(test);
                Console.ReadKey();
            }
    
            public static Uri GetCrmUrl(string phone, string organisationid, string server)
            {
                string clienturl = @"https://petsolutions.crm8.dynamics.com/nga/engagementhub.aspx?org=" + organisationid + "&server=" + server;
                Uri x = new Uri(clienturl, UriKind.Absolute);
                return x;
            }
        }
    }
    

    返回:

    https://petsolutions.crm8.dynamics.com/nga/engagementhub.aspx?org=orgid&server=server

    根本不需要删除斜线。

    【讨论】:

    • 嗨,您的代码完全返回了我想要的。但是,如果我们将其返回到网页,则会发生相同的斜线问题。感谢您的回答。
    【解决方案3】:

    试试这个:

      return new Uri(HttpUtility.HtmlDecode(clienturl).ToString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多