Eliminate URLs from server-side route values

I am trying to create a ValidationAttribute (for checking Remote, which only works on the server side) and inside the IsValid method, I need to resolve the URL from the route values โ€‹โ€‹of the URL. Here is my initial setup:

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?

+4
source share
1 answer
 var httpContext = new HttpContextWrapper(HttpContext.Current); var urlHelper = new UrlHelper(new RequestContext(httpContext, new RouteData())); var url = urlHelper.Action(Action, Controller, RouteValues); 
+7
source

Source: https://habr.com/ru/post/1379854/


All Articles