如何在Umbraco 6.0 for MVC中实现远程验证

时间:2013-07-17 06:05:53

标签: c# asp.net-mvc validation umbraco umbraco6

内容项目网址为http://www.mysite.com/us/signup,其中提供了如下所述的错误

错误消息 No url for remote validation could be found.

验证属性位于模型中的属性

[Remote("IsStoreExists", "Stores", AdditionalFields = "StoreId", ErrorMessageResourceName = "StoreAlreadyExists", ErrorMessageResourceType = typeof(Resources.Stores._CreateOrEdit))]
public string StoreName { get; set; }

我已经尝试过这里提到的mvcbridge调整Calling Actions on a Controller via URL using MVCBridge(不是包,而是关于为控制器添加新路由的想法)。请注意我已将此{Umbraco 6.0应用程序中的HttpApplication覆盖为public class MvcApplication : UmbracoApplication,它将调用RouteConfig类,如下所示。

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

        RouteTable.Routes.MapRoute(
            "Stores", // Route name call it anything you want
            "Stores/{action}/{id}", // URL with parameters,
            new { controller = "Stores", action = "IsStoreExists", id = UrlParameter.Optional }
            );

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

它似乎正在生效,因为当我注释掉名为Stores的自定义路线时,空白页面来自Umbraco中View的空白模板,但是当我取消注释路线Stores时它在网址http://www.mysite.com/us/stores/

显示404

请帮助。

2 个答案:

答案 0 :(得分:0)

可能只是我,但您配置的路线与您使用的网址不同。您的网址以/us/开头,但这是路径中缺少路径的一部分,因此会抛出404.

您可以将路线更改为“我们/商店/ {action} / {id}”,看看是否有效。

答案 1 :(得分:0)

[Remote("IsStoreExists", "StoresSurface", AdditionalFields = "StoreId", HttpMethod = "POST", ErrorMessageResourceName = "StoreAlreadyExists", ErrorMessageResourceType = typeof(Resources.Stores._CreateOrEdit))]
public string StoreName { get; set; }

匹配的表面控制器如下所示:

using System.Web.Mvc;

using Umbraco.Web.Mvc;

/// <summary>   Stores controller. </summary>
public sealed class StoresSurfaceController : SurfaceController
{
    /// <summary>   Does the store exist. </summary>
    /// <param name="StoreName"> Name of the store. </param>
    /// <param name="StoreId"> Identifier of the store. </param>
    /// <returns>   true if the store exists, false if not. </returns>
    [HttpPost]
    public JsonResult IsStoreExists(string StoreName, long StoreId)
    {
        return this.Json(true);
    }
}

这是我经常使用的模式,应该解决您的问题。