创建 WebAPI 以允许使用“/”字符传递字符串
Creating WebAPI to allow a string to be passed with '/' characters
我正在 ASP.NET 中创建 WebApi。我希望我的 get 方法之一允许将字符串传递给它。该字符串将用作 TFS 中分支的路径,我将在其中执行 QueryHistory
到 return 其历史记录。
在我的 WebApiConfig.cs
文件中:
config.Routes.MapHttpRoute(
name: "Branches",
routeTemplate: "api/branches/{fullPath}",
defaults: new { controller = "branches", fullPath = RouteParameter.Optional }
);
在我的 controller.cs 文件中:
// GET api/branches/$/Project/Branches/Path/To-This-Branch
public string Get(Uri fullPath)
{
string output = "";
NetworkCredential cre = new NetworkCredential("COSMO\pd-srv", pWord);
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://fortknox:8080/tfs/PD"), cre);
var service = tfs.GetService<VersionControlServer>();
string s = System.Uri.UnescapeDataString(fullPath);
var latestChange = service.QueryHistory(s, RecursionType.None, 1);
//output = JsonConvert.SerializeObject(latestChange);
//return a json formatted string containing the full history of the branch path passed in
return "value";
}
我尝试了多种方法,当我传入一个 uriEncoded 参数时,UnescapeDataString
似乎只接受字符串,所以它不起作用。如果我传入字符串,由于 '/'
,我会收到错误消息。
如何将此路径从我的 javascript 传递到我的 api 中的 Get()
调用?
您可以在路由模板末尾使用 {*fullPath}
以使用通配符表达式匹配 fullPath
。
config.Routes.MapHttpRoute(
name: "Branches",
routeTemplate: "api/branches/{*fullPath}",
defaults: new { controller = "branches", fullPath = RouteParameter.Optional }
);
见MSDN: Handling a Variable Number of Segments in a URL Pattern
我过去也曾 运行 解决过这个问题,并通过将“/”转换为“!”解决了这个问题。然后在 API 中转换回来。你可以在那里替换任何字符......你只需要摆脱“/”
尝试调用 Get 方法:
api/branches?fullPath="directory1/directory2"
我正在 ASP.NET 中创建 WebApi。我希望我的 get 方法之一允许将字符串传递给它。该字符串将用作 TFS 中分支的路径,我将在其中执行 QueryHistory
到 return 其历史记录。
在我的 WebApiConfig.cs
文件中:
config.Routes.MapHttpRoute(
name: "Branches",
routeTemplate: "api/branches/{fullPath}",
defaults: new { controller = "branches", fullPath = RouteParameter.Optional }
);
在我的 controller.cs 文件中:
// GET api/branches/$/Project/Branches/Path/To-This-Branch
public string Get(Uri fullPath)
{
string output = "";
NetworkCredential cre = new NetworkCredential("COSMO\pd-srv", pWord);
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://fortknox:8080/tfs/PD"), cre);
var service = tfs.GetService<VersionControlServer>();
string s = System.Uri.UnescapeDataString(fullPath);
var latestChange = service.QueryHistory(s, RecursionType.None, 1);
//output = JsonConvert.SerializeObject(latestChange);
//return a json formatted string containing the full history of the branch path passed in
return "value";
}
我尝试了多种方法,当我传入一个 uriEncoded 参数时,UnescapeDataString
似乎只接受字符串,所以它不起作用。如果我传入字符串,由于 '/'
,我会收到错误消息。
如何将此路径从我的 javascript 传递到我的 api 中的 Get()
调用?
您可以在路由模板末尾使用 {*fullPath}
以使用通配符表达式匹配 fullPath
。
config.Routes.MapHttpRoute(
name: "Branches",
routeTemplate: "api/branches/{*fullPath}",
defaults: new { controller = "branches", fullPath = RouteParameter.Optional }
);
见MSDN: Handling a Variable Number of Segments in a URL Pattern
我过去也曾 运行 解决过这个问题,并通过将“/”转换为“!”解决了这个问题。然后在 API 中转换回来。你可以在那里替换任何字符......你只需要摆脱“/”
尝试调用 Get 方法:
api/branches?fullPath="directory1/directory2"