我想Point
使用属性路由和[FromUri]
属性将URL参数绑定到我的对象,以便可以使用以下URL:
/ foo-1,2
public IHttpActionResult PostFoo(
[FromBody] string content,
[FromUri] Point point)
{
}
public class Point
{
public int A { get; set; }
public int B { get; set; }
// ...Other properties omitted for simplicity
}
我尝试了以下Route属性,但是这些都不起作用:
[Route("foo-{a},{b}")]
[Route("foo-{A},{B}")]
[Route("foo-{point.A},{point.B}")]
请注意,我不能使用查询字符串参数,因为构建错误的第三方服务不会在其URL中接受“&”号(是的,这很糟糕)。因此,我正在尝试将所有查询字符串参数构建到URL本身。
我知道的两个选项是:
使用URL重写器可以在全球范围内处理所有所有路线。优点是(我希望)您的发布者确实具有某种类型的标准url,您可以将其转换为友好的MVC路由。
如果没有,那么您可能必须编写自己的RouteHandler。不知道您是否可以在全球范围内使用它,但是您必须进行大量注册(实际上并不难)。
public class CustomRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var acceptValue = requestContext.HttpContext.Request.Headers["Accept"];
if( /* do something with the accept value */)
{
// Set the new route value in the
// requestContext.RouteData.Values dictionary
// e.g. requestContext.RouteData.Values["action"] = "Customer";
}
return base.GetHttpHandler(requestContext);
}
}
然后注册:
RouteTable.Routes.MapRoute(
name: "Custom",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
).RouteHandler = new CustomRouteHandler();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句