newbieCSharp
newbieCSharp

Reputation: 191

Escape backward slash in URL

Is it possible to make a call to web API that accepts string to escape backward slash?

The syntax for call is /api/testapi/PostSomeData/{ID}/{text}. text may be URL, file path or just some text. If the text is URL or any text my program works fine. If it is file path like file:///C:/TestDirec or \\\\ServerName\\SomeData\\dinosaur.jpg then it fails.

http://localhost:12/api/testapi/PostSomeData/1/file:///C:/TestDirec

also my WebApiConfig file looks like this

config.Routes.MapHttpRoute(
                 name: "Api",
                 routeTemplate: "api/testapi/{action}/{ID}/{FilePath}",
                 defaults: new { Controller = "testapi", ID = @"\d+", FilePath =@"\d+" }
             );

Controller code is

[AcceptVerbs("GET")]
public void PostSomeData(int ID, string FilePath)
{
}

How to do this?

Upvotes: 3

Views: 2787

Answers (1)

EZI
EZI

Reputation: 15364

Use WebUtility.UrlEncode or HttpUtility.UrlEncode

HttpUtility.UrlEncode("file:///C:/TestDirec")

it will return file%3a%2f%2f%2fC%3a%2fTestDirec which is a valid string to use in a URL.

Upvotes: 3

Related Questions