旧
我为我的应用程序创建了一个新路由,如下所示
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。当我想要回家/登录时,只需重新加载页面即可。这在新路线之前有效。
我的问题是,我是否需要为每种方法创建一条新路线,或者有更好的方法吗?
更新
我想要的是这样的网址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
我遇到的问题是,如果我导航到可以说'订阅/索引' '定制'已离开网址,如果我删除默认路由,则不会转到上述任何一个网址。
答案 0 :(得分:0)
默认路由仅适用于应用程序启动时您可以使用:
@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
}
});
很有用
答案 1 :(得分:0)
我找到了解决问题的方法。
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 }
);
}