ASP.NET MVC针对不同的动作进行不同的路由

时间:2013-06-18 05:50:06

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我正在使用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,因为我想要不同导演和不同电影的不同网址,以满足我们客户的SEO requirement

它适用于Index操作,但即使我尝试使用Create调用@Html.ActionLink("Create New", "Create")操作,它仍然会调用Index操作。根据我到目前为止的理解,它应该调用Create动作。我是MVC的新手,很抱歉,如果我的问题看起来很愚蠢。但是重要的是,我在这里失踪了?

2 个答案:

答案 0 :(得分:4)

自上而下检查Routeconfig。

更新:您应该在路由中指定控制器名称,否则就像您的问题中路由配置的方式一样,Default2永远不会被触发。有两条路线以{something}开头意味着第二条路线不会被解雇。我猜您希望自己的网址为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 }
     ); 
}

答案 1 :(得分: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 }
             );

}

我不是百分百肯定,你需要一些修改。但是你不能传递一个类似的url模式并调用两个控制器。

根据你问题中的代码,如果网址匹配,它将始终调用第一条路线。并且您也使用相同的模式到第二条路线。总是隐藏第二条路线。请检查并告知我们。