简体   繁体   中英

Remote Validation Incorrect URL

I am using MVC's remote validation in my web application. When I attempt to use the Remote[] validation attribute

[Remote("EmailExists", "Validation")]
public string EmailAddress { get; set; }

I expect this email address to reach out to my ValidationController and call the method EmailExists .

public class ValidationController : Controller
{
    public JsonResult EmailExists(string emailAddress)
    {
        return Membership.GetUser(emailAddress) != null ?
            Json("true", JsonRequestBehavior.AllowGet) :
            Json("Email does not exist", JsonRequestBehavior.AllowGet);
    }
}

With the rendered HTML looking something like this

<input class="form-control" data-val="true" data-val-remote="'EmailAddress' is invalid." data-val-remote-additionalfields="*.EmailAddress" data-val-remote-url="/IdSrv/Validation/EmailExists" id="EmailAddress" name="EmailAddress" type="text" value="">

But instead, if you look at the data-val-remote-url you will notice it renders something completely different when the HTML actually renders

<input class="form-control" data-val="true" data-val-remote="'EmailAddress' is invalid." data-val-remote-additionalfields="*.EmailAddress" data-val-remote-url="/IdSrv/issue/wstrust?controller=Validation&amp;action=EmailExists" id="EmailAddress" name="EmailAddress" type="text" value="">

I am using ThinkTecutre Identity Provider, but I'm not sure why the default route set up by the Remote validation is incorrect. Am I doing something wrong, or is there something I can change to point the URL to the correct location?

Make sure you don't any custom routing defined in RoutingConfig.cs before default routing.

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
}

Try matching Property name with action method parameter

public JsonResult EmailExists(string EmailAddress)
{
}

Check below links for more details.

http://msdn.microsoft.com/en-us/library/gg508808%28vs.98%29.aspx

http://www.codeproject.com/Tips/669824/Implementing-Remote-Validation-in-MVC

http://www.codeproject.com/Articles/674288/Remote-Validation-in-MVC-Simple-Way-to-Pass-the-F

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM