Haminteu
Haminteu

Reputation: 1334

MVC Passing back Slash to URL

I have a controller like:

    public ActionResult Edit(string id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        SecurityUser securityUser = db.SecurityUsers.Find(id);
        if (securityUser == null)
        {
            return HttpNotFound();
        }
        return View(securityUser);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "UserId,UserName,UserEmail")] SecurityUser securityUser)
    {
        if (ModelState.IsValid)
        {
            db.Entry(securityUser).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(securityUser);
    }

The Id is a string which has a back-slash \ on it. Like, Domain\account.
How can I pass this Id to URL?

Need idea please. Thank you.

Upvotes: 0

Views: 69

Answers (1)

Ljubomir Bacovic
Ljubomir Bacovic

Reputation: 584

You need to URL-encode it (to %5C). See the table here:

https://www.w3schools.com/tags/ref_urlencode.asp

To do that, you can use UrlHelper.Encode

Check documentation:

https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.urlhelper.encode?view=aspnet-mvc-5.2

Upvotes: 1

Related Questions