Erki M.
Erki M.

Reputation: 5072

Failing to replace backslash

I have a string coming in to a method that represents domain username, from razor view, called via JS:

    <script type="text/javascript">
        function editResource() {
...
            var user = '@Context.User.Identity.Name';
            $.post(url, ... skipped
            });
        }
    </script>

Controller:

public string SaveDeprecatedResource(string resource, string newResource, string description, string user, string ticket)
    {
        user = user.Replace("\\", "\\\\"); // how do I replace with double backslash?

user that comes to controller equals "DOMAIN\v-user"

user object is string:

user.GetType()
{Name = "String" FullName = "System.String"}
    [System.RuntimeType]: {Name = "String" FullName = "System.String"}

I need to insert it insert it to db (LINQ), but it eats the backslash. Basically I need to replace the \ with \\, I am failing to do so. What am I doing wrong?

user.Contains(@"\")
false
user.Contains(@"\\")
false
user.Contains("\\")
false
user.Contains('\\')
false

EDIT, so the backslash is not a backslash, but ie \v is treated as vertical tab. Anyway, after the backslash can be any letter of the alphabet. How do I universally handle it?

Upvotes: 0

Views: 209

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

You need to escape backslash when you are saving the data into DB.

user = @"DOMAIN\user"

Upvotes: 2

Related Questions