Why backslashes are removed from the request - ASP.NET and Javascript

I am trying to create a url containing a UNC path as one of the query string variables. The URL will open in a popup when the ASP.NET user control clicks on the user. When a button is clicked, backslashes are removed from the UNC path, resulting in a page break.

The button displays correctly in the original page file with all backslashes.

Is there any way to prevent this?

Here is my source code:

Code behind:

string unc = @"\\myserver\myfolder\myfile.txt";
string url = string.Format("http://www.mysite.com/page.aspx?a={0}", unc);

MyButton.Attributes.Add("onclick", @"javascript:FullPop('" + url + @"')");

Aspx page

<script language="javascript" type="text\javascript">
    function FullPop(Newurl) {
        Win = window.open( Newurl,"Monitor", "fullscreen=0,toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=800,height=600,top=50,left=50");
        Win.focus();
    }
</script>

<asp:button id="MyButton" runat="server" cssclass="mycss" text="View Actual Target" />

Update

Server.UrlEncode is not working. The same behavior.

Update 1

Based on Daniel Lew's answer, I developed the following solution:

protected void Page_Load(object sender, EventArgs e)
{  
    string unc = @"\\myserver\myfolder\myfile.txt";
    string url = string.Format("http://www.mysite.com/page.aspx?a={0}", unc);

    MyButton.Attributes.Add("onclick", @"javascript:FullPop('" + this.EscapeforJavaScript(url) + @"')");
}

private string EscapeforJavaScript(string url)
{
    return url.Replace(@"\", @"\\"); 
}
+3
source share
3

URL-, , URL-:

string url = "http://www.mysite.com/page.aspx?a=" + Server.UrlEncode(unc);

Edit:
URL- Javascript-, :

MyButton.Attributes.Add("onclick", @"FullPop('" + url.Replace(@"\", @"\\").Replace("'", @"\'") + @"')");

( javascript: , Javascript href , , onclick.)

+4

asp.net, JavaScript . URL-, ?

// Returns "\myservermyfoldermyfile.txt", due to escpaing the backslash.
alert("\\myserver\myfolder\myfile.txt");

// Returns correct value of "\\myserver\myfolder\myfile.txt"
alert("\\\\myserver\\myfolder\\myfile.txt");
+1

, URLEncoding , :

public static string UrlFullEncode(string strUrl) 
{
    if (strUrl == null)
        return "";
    strUrl = System.Web.HttpUtility.UrlEncode(strUrl);
}

, 100% , .

0

Source: https://habr.com/ru/post/1707630/


All Articles