Created
April 14, 2012 01:30
-
-
Save jsakamoto/2381357 to your computer and use it in GitHub Desktop.
ASP.NET MVC Data Annotation Valiteion - Remote Validation with Server Side Validation!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTICE: | |
// This is "concept" sample code. | |
// Not enough resouce disposing, etc. | |
public class MyRemoteAttribute : RemoteAttribute | |
{ | |
public RemotableCustomAttribute(string action, string controller) : base(action, controller) | |
{ | |
} | |
public string ControllerName { get { return this.RouteData["controller"] as string; } } | |
public string ActionName { get { return this.RouteData["action"] as string; } } | |
// | |
// Server side validation here! | |
// | |
public override bool IsValid(object value) | |
{ | |
if (value == null) return true; | |
// Create instance of ASP.NET MVC Controler. | |
var context = new HttpContextWrapper(HttpContext.Current); | |
var routeData = new RouteData(); | |
var requestContext = new RequestContext(context, routeData); | |
var controller = ControllerBuilder.Current | |
.GetControllerFactory() | |
.CreateController(requestContext, controllerName); | |
// Invoke action method, and eval return value. | |
var result = controller.GetType().InvokeMember( | |
this.ActionName, | |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, | |
null, | |
controller, new object[] { value } | |
) as JsonResult; | |
var isValid = (result.Data is bool) && ((bool)result.Data); | |
return isValid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SOLUTION TO THIS PROBLEM:
//var context = new HttpContextWrapper(HttpContext.Current);
//var routeData = new RouteData();
//var requestContext = new RequestContext(context, routeData);
var requestContext = HttpContext.Current.Request.RequestContext;