C# 从服务器端的路由值解析URL
我正在尝试为远程验证创建一个ValidationAttribute,它只在服务器端和IsValid方法内部工作,我需要从url路由值解析url。以下是我的初始设置:C# 从服务器端的路由值解析URL,c#,asp.net,.net,asp.net-mvc,asp.net-mvc-validation,C#,Asp.net,.net,Asp.net Mvc,Asp.net Mvc Validation,我正在尝试为远程验证创建一个ValidationAttribute,它只在服务器端和IsValid方法内部工作,我需要从url路由值解析url。以下是我的初始设置: public class ServerSideRemoteAttribute : ValidationAttribute { public string Controller { get; set; } public string Action { get; set; } public object Rou
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吗?比如:localhost:6738/Home/Index/100?@tugberk,不会解析为完整的URL。如果需要绝对url,则必须使用适当的重载:var url=urlHelper.ActionAction、Controller、routeValue、urlHelper.RequestContext.HttpContext.Request.url.Scheme;再次感谢。我应该看看MSDN,而不是懒惰:s
var httpContext = new HttpContextWrapper(HttpContext.Current);
var urlHelper = new UrlHelper(new RequestContext(httpContext, new RouteData()));
var url = urlHelper.Action(Action, Controller, RouteValues);