2012-05-21 168 views
0

在asp.net mvc的使用缺省路由和使用ActionLink的ASP.NET MVC 3路由问题

@Html.ActionLink("title", "Index", "Question", new { id = 25}, null) 

的结果是:

http://localhost/question/index/25 

改变链接

http://localhost/question/25 

我在Global.asax中默认添加了新的路由角色:

routes.MapRoute(
      "default2", // Route name 
      "Question/{id}", // URL with parameters 
      new { controller = "Questions", action = "Index", id = UrlParameter.Optional} // Parameter defaults 
     ); 

我有同样的问题用户,标签,...,我应该为每个主题创建相同的角色吗?

+0

你在哪里添加路由?如果您在{controller}/{action}/{id}的默认MVC路径之后添加了它,则需要将其移至默认路径上方。此外,你不能有两个具有相同名称的路线,所以我建议调用这条路线“问题”或什么。 –

+0

@NickBork,你说得对。我在'default'之前添加了这条路线。和名称不同(更新后) – Mironline

+1

那么移动路由位置会导致ActionLink生成正确的actionlink代码?如果是这样,您可以创建另外两条路线,一条用于“用户”,另一条用于“标签”,或者您可以修改新路线并使用路线约束进行一些过滤以仅匹配“问题”,“用户”或“标签” –

回答

2

你试过吗?

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

UPDATE:

如果id始终将是一个整数,那么你可以把一个简单的数字限制在上面的路线,以避免@Nick报道的路由问题。

routes.MapRoute(
     "my-route", 
     "{controller}/{id}", 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     new { id = @"\d*" } 
); 
+0

是的,我试过了。但我认为这条路线直接影响'形式'。我不知道为什么,但是当我使用这条路线时,'action'属性的形式是空的**! – Mironline

+1

如果使用此路由,{controller}/{action}/{id}的默认路由永远不会匹配,因为您的新路由始终是匹配项,每个URL都会查找索引操作。为了使这条路线起作用,您需要创建一个路由约束来确保ID是有效的(数字值或数据库中存在的)。 –

1

我想我会借此更进一步,向您展示如何创建一个路由约束,所以你并不需要注册三个独立的路线。

使用下面的文章为指导,你可以创建一个约束,将验证对控制器列表当前路由控制器将指定:

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs

因此,这里是我的类路径约束:

public class ControllerConstraint : IRouteConstraint 
{ 
    private string[] _controllers; 

    public ControllerConstraint() : this(null) { } 
    public ControllerConstraint(string[] controllers) 
    { 
     _controllers = controllers; 
    } 

    #region IRouteConstraint Members 
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
    { 
     string currentController = values.ContainsKey("controller")? values["controller"].ToString() : null; 

     return _controllers != null //The list of controllers passed to the route constraint has at least one value in it 
      && !String.IsNullOrEmpty(currentController) //The current route data has a controller in it to compare against 
      && (from c in _controllers where c.Equals(currentController,StringComparison.CurrentCultureIgnoreCase) select c).ToList().Count > 0; //We find a match of the route controller against the list of controllers 
    } 
    #endregion 
} 

从那里,你需要做的是修改你如何在Globa.asax

注册您的路线3210
routes.MapRoute(
     "Action-less Route", // Route name 
     "{controller}/{id}", // URL with parameters 
     new { controller = "Questions", action = "Index", id = UrlParameter.Optional}, //Parameter defaults 
     new {isController = new ControllerConstraint(new string[] {"Questions","Users","Tags"})} //Route Constraint 
     ); 

你也可以把它更进一步,验证(编号)与附加的路由约束类似下面的号码:

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs