从服务器端的路由值解析url

本文关键字:url 路由 服务器端 | 更新日期: 2023-09-27 18:13:34

我正在尝试创建一个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);
    }
}

任何想法吗?

从服务器端的路由值解析url

var httpContext = new HttpContextWrapper(HttpContext.Current);
var urlHelper = new UrlHelper(new RequestContext(httpContext, new RouteData()));
var url = urlHelper.Action(Action, Controller, RouteValues);