C# MVC 为所有方法创建路由?

C# MVC Create route for all methods?

我已经为我的应用创建了一个新路由,如下

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

虽然有效,但仅适用于 Home/Index。当我想转到 Home/Login 时,它只是重新加载页面。这在新路线之前有效。

我的问题是,我是否需要为每种方法创建一个新路由,或者有更好的方法吗?

更新

我想要得到的是这样的 url localhost/{custom}/{controller}/{action}/{id} 其中 'custom' 是从公司获取信息的唯一 ID。

1234 = company 'A' -> localhost/1234/Home/Index
0987 = company 'B' -> localhost/0987/Home/Index

这是我的完整路线

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

    routes.MapRoute(
        name: "Route1",
        url: "{custom}/{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        constraints: new { custom = new CustomerServiceModule.Helpers.RouteValidation() }
    );

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

}

这是我的 RouteValidation,它检查 {custom} 是否是一个有效的 id,这个有效。

public class RouteValidation : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {                        
        if (routeDirection == RouteDirection.IncomingRequest)
        {
            //returns true if the 'custom' is valid else false
            return CheckPublicationUniqueID(values["custom"].ToString());
        }

        return false;
    }
}

下面是应用的一些路由

Index/Home
Index/Login
Subscription/Index
Subscription/Edit/{id}
Subscription/Details/{id}
Invoice/Index
History/Index

我遇到的问题是,如果我导航到 'Subscription/Index','custom' 就会从 url 中消失,如果我删除默认路由,它不会转到以上任何一个名为 urls.

默认路由仅供您启动应用程序时使用:

@Html.RouteLink("LINKTEXT", new { action = "ACTION", controller = "CONTROLLER", area = "AREA IF THERE IS ONE" })

这将在您的导航中。

希望这对您有所帮助。

编辑

如果您愿意,可以将它们全部放在 RouteConfig.cs 中,但它只是杂乱无章而且不需要。如果您的调用方法与导航无关,您需要一种完全不同的方法,例如 ajax:

$.ajax({
    type: "POST",
    url: "/Controller/Method",
    success: function (data) {
          // do bits
    }
});

这对那个很有用http://www.w3schools.com/jquery/ajax_ajax.asp

好吧,我找到了解决问题的方法。

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

        routes.MapRoute(
            "CompanyRouteWithoutParameters",
            "{company}/{controller}/{action}",
            new { controller = "Home", action = "Index" },
            new { company = new RouteValidation() }
        );

        routes.MapRoute(
            "CompanyRouteWithParameters",
            "{company}/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }