1

我正在使用 ASP.NET MVC 构建一个站点。在RouteConfig,我修改了这样的方法:

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     routes.MapRoute(
         name: "Default",
         url: "{Director}/{Movie}/{id}",
         defaults: new { controller = "Movies", action = "Index", id = UrlParameter.Optional, Director = UrlParameter.Optional, Movie = UrlParameter.Optional }
         );

     routes.MapRoute(
         name: "Default2",
         url: "{controller}/{action}/{id}",
         defaults: new { controller = "Movies", action = "Create", id = UrlParameter.Optional }
         );            
}

在 IndexView 中,我编码如下:

@model IEnumerable<MvcMovie.Models.Director>

<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
        </tr>
        <tr>
            <td>
                @foreach (var movie in item.Movies)
                {
                    <div style="width: 100%;">
                        @Html.ActionLink(movie.Title, "Index", new { Director = movie.Director.Name, Movie = movie.Title }, null)
                    </div>
                }
            </td>
        </tr>
    }
</table>

实际上我修改RouteConfig是因为我想要不同导演和不同电影的不同 URL 以满足我们客户的SEO requirement.

它对IndexAction 工作正常,但即使我尝试Create使用 调用动作@Html.ActionLink("Create New", "Create"),它仍然会调用Index动作。根据我目前的理解,它应该调用CreateAction。如果我的问题看起来很愚蠢,我是 MVC 的新手,很抱歉。但是什么重要的事情,我在这里失踪了?

4

2 回答 2

4

Routeconfig 是自上而下检查的。

更新:您应该在路由中指定控制器名称,否则您的 routeconfig 在您的问题中的方式,Default2 永远不会被触发。有 2 个路由以 {something} 开头意味着第二个不会被触发。我猜你希望你的 URL 是localhost/movies/create/id. 为此,请执行以下操作:

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     routes.MapRoute(
         name: "MoviesDefault",
         url: "Movies/{action}/{id}",
         defaults: new { controller = "Movies", action = "Index", id = UrlParameter.Optional }
         );  

     /* not sure about this route without testing - 
     /* i think it would conflict with the above route
     routes.MapRoute(
         name: "MovieDirector",
         url: "Movies/{Director}/{Movie}/{id}",
         defaults: new { controller = "Movies", action = "Index", id = UrlParameter.Optional, Director = UrlParameter.Optional, Movie = UrlParameter.Optional }
         );
     */

     // for this following route, i've left 'Movies' as the controller, but it should be 
     // your 'home page' controller. As in, whatever your default http://localhost:port/ should be.
     routes.MapRoute(
         name: "Default",
         url: "{controller}/{action}/{id}",
         defaults: new { controller = "Movies", action = "Index", id = UrlParameter.Optional }
     ); 
}
于 2013-06-18T05:52:34.627 回答
0

请您按照此修改并检查。

我相信当网址是

//http://localhost/Movies/Create/1 -> invokes Movies controller and Create action.

 public static void RegisterRoutes(RouteCollection routes)
    {
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
         routes.MapRoute(
             name: "MoviesCreate",
             url: "Movies/{Movies}/Create/{id}",
             defaults: new { controller = "Movies", action = "Create", id = UrlParameter.Optional }
             );    
//http://Movies/JCameron/Titanic/12 -> invokes Movies controller and Index action.

         routes.MapRoute(
             name: "MoviesHome",
             url: "Movies/{Director}/{Movie}/{id}",
             defaults: new { controller = "Movies", action = "Index", id = UrlParameter.Optional, Director = UrlParameter.Optional, Movie = UrlParameter.Optional }
             );

}

我不是 100% 确定,你需要一些修改。但是您不能传递类似类型的 url 模式并调用两个控制器。

根据您问题中的代码,如果 url 匹配,它将始终调用第一条路由。而且您也在第二条路线上使用相同的模式。所以第二条路线总是隐藏的。请检查并让我知道。

于 2013-06-18T06:07:15.060 回答