我正在开发一个 ServiceStack api,但无法路由到:
[BsonId]
public ObjectId Id { get; set; }
我尝试按如下方式设置自定义绑定模型:
public class ObjectIdModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
return result == null ? ObjectId.Empty : ObjectId.Parse((string)result.ConvertTo(typeof(string)));
}
}
protected void Application_Start()
{
ModelBinders.Binders.Add(typeof(ObjectId), new ObjectIdModelBinder());
这是在 AppHost.cs
路由 .Add("/user") .Add("/user/{Id}");
当我尝试通过 url 访问 api 时:
http://localhost:1000/api/user/1234567
我收到以下错误:
错误代码 RequestBindingException 消息 Unable to bind request stack Trace at ServiceStack.WebHost.Endpoints.RestHandler.GetRequest(IHttpRequest httpReq, IRestPath restPath) at ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName)
绑定到基本的本机类型有效,知道如何解决这样的问题吗?