我正在尝试创建ValidationAttribute (用于远程验证,仅适用于服务器端)并且在IsValid方法内部,我需要从url路由值中解析url。这是我的初始设置:
public class ServerSideRemoteAttribute : ValidationAttribute {
public string Controller { get; set; }
public string Action { get; set; }
public object RouteValues { get; set; }
public ServerSideRemoteAttribute(string controller, string action) {
this.Controller = controller;
this.Action = action;
}
public ServerSideRemoteAttribute(string controller, string action, object routeValues) {
this.Controller = controller;
this.Action = action;
this.RouteValues = routeValues;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
//Here I need to resolve the url in order to make a call to that controller action and get the JSON result back
return base.IsValid(value, validationContext);
}
}
有什么想法吗?
答案 0 :(得分:7)
var httpContext = new HttpContextWrapper(HttpContext.Current);
var urlHelper = new UrlHelper(new RequestContext(httpContext, new RouteData()));
var url = urlHelper.Action(Action, Controller, RouteValues);