Resolving urls from Route Values on server side
-
22-02-2021 - |
Frage
I am trying to create a ValidationAttribute (for Remote validation which only works on the server side) and inside the IsValid method, I need to resolve the url from url route values. Here is my initial set up :
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);
}
}
Any thoughts?
Lösung
var httpContext = new HttpContextWrapper(HttpContext.Current);
var urlHelper = new UrlHelper(new RequestContext(httpContext, new RouteData()));
var url = urlHelper.Action(Action, Controller, RouteValues);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow