servicestack validation
I have a model
[Validator(typeof(ContractValidator))]
[Route("/contracts", "POST")]
public class Contract
{
public int ContractID{get; set;}
public string ContractNumber{get;set;}
}
I also have a validation class for the model above.
public class ContractValidator : AbstractValidator<Contract>
{
public ContractValidator()
{
RuleFor(x => x.ContractNumber).Length(0, 10);
}
}
I also have a class to map a 'GET' request:
[Route("/contracts/{ContractID}", "GET")]
public class GetContract
{
public int ContractID { get; set; }
}
...and a service:
public class ContractService : ServiceStack.ServiceInterface.Service
{
public object Get(GetContract request)
{
IContractRepository repository = new ContractRepository();
return repository.GetById(request.ContractID);
}
public object Post(Contract request)
{
IContractRepository repository = new ContractRepository();
repository.Save(request);
return request;
}
}
Now in my Razor view to edit a contract object I have
@inherits ViewPage . . .
But the problem is I'm getting the following error when I try to save and there is a model validation error:
Unable to cast object of type 'ServiceStack.ServiceInterface.ServiceModel.ErrorResponse' to type 'DTO.Contract'
Can someone please let me know how to handle this?
Thanks,
Ermias.
I've just committed a fix for this so you should no longer get this error with the next version of ServiceStack in HEAD now (or at the end of this week on NuGet).
The new behaviour is any Exception will populate the ModelError
property on the Razor View. The @Model
will be null, but you can access the typed errors by looking at the base.ResponseStatus
property on the View Page.
下一篇: 服务堆栈验证