UrlHelper.Link returns 不需要的参数
UrlHelper.Link returns undesired parameters
如果我在 API 调用中调用 UrlHelper.Link,该调用的参数与 API 端点的可选参数相匹配,我正在尝试获取 URL , UrlHelper.Link returns 具有当前请求值的 URL 无论我如何尝试从 link.
中排除可选参数
例如
[HttpGet]
[Route("api/Test/Stuff/{id}")]
public async Task<IHttpActionResult> GetStuff(int id) // id = 78
{
string link = Url.Link("Mary", new
{
first = "Hello",
second = "World"
});
string link2 = Url.Link("Mary", new
{
first = "Hello",
second = "World",
id = (int?)null
});
string link3 = Url.Link("Mary", new
{
first = "Hello",
second = "World",
id = ""
});
return Ok(new
{
link,
link2,
link3
});
}
[HttpGet]
[Route("api/Test/Another/{first}", Name = "Mary")]
public async Task<IHttpActionResult> AnotherMethod(
[FromUri]string first,
[FromUri]string second = null,
[FromUri]int? id = null)
{
// Stuff
return Ok();
}
获取http://localhost:53172/api/Test/Stuff/8
returns
{
"link": "http://localhost:53172/api/Test/Another/Hello?second=World",
"link2": "http://localhost:53172/api/Test/Another/Hello?second=World&id=8",
"link3": "http://localhost:53172/api/Test/Another/Hello?second=World&id=8"
}
如何让 Url.Link 实际使用您传递给它的值,而不是在它们未显示或分配给 null 或空字符串时从当前 api 请求中提取它?
我认为这个问题与....
非常相似
UrlHelper.Action includes undesired additional parameters
但这是 Web API 不是 MVC,不是动作,所提供的答案似乎并没有为这个问题提供明显的解决方案。
编辑: 我已经更新了代码,因为原始代码实际上并没有重现这个问题。我还包含了一个请求 URL 和返回的响应,我已经对其进行了测试。虽然这段代码演示了这个问题,但我试图找到解决方法的代码并没有将匿名类型传递给 UrlHelper,而是将其 class 生成时间戳和哈希值并具有 4 个可选参数。如果有另一种解决方案不需要省略传递给 UrlHelper 的结构中的可选参数,我想拥有它,因为它可以避免我对代码进行大量更改。
传递路由值时不要包含 id
。事实上它不应该让你编译为
Cannot assign <null>
to anonymous type property
public async Task<IHttpActionResult> GetStuff(int id) // id = 78 {
string myUrl = Url.Link(
"Mary",
new
{
first = "Hello World"
});
//...other code
}
已通过网络 api 2 进行测试,link 进行其他操作时 id
未包含在内。
[RoutePrefix("api/urlhelper")]
public class UrlHeplerController : ApiController {
[HttpGet]
[Route("")]
public IHttpActionResult GetStuff(int id) {
string myUrl = Url.Link(
"Mary",
new {
first = "Hello World",
});
return Ok(myUrl);
}
[HttpGet]
[Route(Name = "Mary")]
public IHttpActionResult AnotherMethod(
[FromUri]string first,
[FromUri]string second = null,
[FromUri]int? id = null) {
// Stuff
return Ok();
}
}
通话中
client.GetAsync("/api/urlhelper?id=78")
路由到 GetStuff 操作并生成 link
"http://localhost/api/urlhelper?first=Hello%20World"
即使使用
进行测试
string myUrl = Url.Link(
"Mary",
new {
first = "Hello World",
id = ""
});
id
未包含在生成的 link 中。
如果我在 API 调用中调用 UrlHelper.Link,该调用的参数与 API 端点的可选参数相匹配,我正在尝试获取 URL , UrlHelper.Link returns 具有当前请求值的 URL 无论我如何尝试从 link.
中排除可选参数例如
[HttpGet]
[Route("api/Test/Stuff/{id}")]
public async Task<IHttpActionResult> GetStuff(int id) // id = 78
{
string link = Url.Link("Mary", new
{
first = "Hello",
second = "World"
});
string link2 = Url.Link("Mary", new
{
first = "Hello",
second = "World",
id = (int?)null
});
string link3 = Url.Link("Mary", new
{
first = "Hello",
second = "World",
id = ""
});
return Ok(new
{
link,
link2,
link3
});
}
[HttpGet]
[Route("api/Test/Another/{first}", Name = "Mary")]
public async Task<IHttpActionResult> AnotherMethod(
[FromUri]string first,
[FromUri]string second = null,
[FromUri]int? id = null)
{
// Stuff
return Ok();
}
获取http://localhost:53172/api/Test/Stuff/8
returns
{
"link": "http://localhost:53172/api/Test/Another/Hello?second=World",
"link2": "http://localhost:53172/api/Test/Another/Hello?second=World&id=8",
"link3": "http://localhost:53172/api/Test/Another/Hello?second=World&id=8"
}
如何让 Url.Link 实际使用您传递给它的值,而不是在它们未显示或分配给 null 或空字符串时从当前 api 请求中提取它?
我认为这个问题与....
非常相似UrlHelper.Action includes undesired additional parameters
但这是 Web API 不是 MVC,不是动作,所提供的答案似乎并没有为这个问题提供明显的解决方案。
编辑: 我已经更新了代码,因为原始代码实际上并没有重现这个问题。我还包含了一个请求 URL 和返回的响应,我已经对其进行了测试。虽然这段代码演示了这个问题,但我试图找到解决方法的代码并没有将匿名类型传递给 UrlHelper,而是将其 class 生成时间戳和哈希值并具有 4 个可选参数。如果有另一种解决方案不需要省略传递给 UrlHelper 的结构中的可选参数,我想拥有它,因为它可以避免我对代码进行大量更改。
传递路由值时不要包含 id
。事实上它不应该让你编译为
Cannot assign
<null>
to anonymous type property
public async Task<IHttpActionResult> GetStuff(int id) // id = 78 {
string myUrl = Url.Link(
"Mary",
new
{
first = "Hello World"
});
//...other code
}
已通过网络 api 2 进行测试,link 进行其他操作时 id
未包含在内。
[RoutePrefix("api/urlhelper")]
public class UrlHeplerController : ApiController {
[HttpGet]
[Route("")]
public IHttpActionResult GetStuff(int id) {
string myUrl = Url.Link(
"Mary",
new {
first = "Hello World",
});
return Ok(myUrl);
}
[HttpGet]
[Route(Name = "Mary")]
public IHttpActionResult AnotherMethod(
[FromUri]string first,
[FromUri]string second = null,
[FromUri]int? id = null) {
// Stuff
return Ok();
}
}
通话中
client.GetAsync("/api/urlhelper?id=78")
路由到 GetStuff 操作并生成 link
"http://localhost/api/urlhelper?first=Hello%20World"
即使使用
进行测试string myUrl = Url.Link(
"Mary",
new {
first = "Hello World",
id = ""
});
id
未包含在生成的 link 中。