C# ASP.NET MVC针对不同操作的不同路由

C# ASP.NET MVC针对不同操作的不同路由,c#,asp.net,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing,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}",

我正在使用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>
@model IEnumerable
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.Name)
@foreach(item.Movies中的var movie)
{
@ActionLink(movie.Title,“Index”,新的{Director=movie.Director.Name,movie=movie.Title},null)
}
}
实际上,我修改了
RouteConfig
,因为我想为不同的导演和不同的电影提供不同的URL,以满足客户的
搜索引擎优化要求


对于
索引
操作,它工作得很好,但即使我尝试使用
@Html.ActionLink(“创建新”、“创建”)
调用
创建
操作,它仍然会调用
索引
操作。根据我目前的理解,它应该调用
Create
操作。我是MVC的新手,如果我的问题看起来很愚蠢,我很抱歉。但是我在这里遗漏了什么重要的东西呢?

Routeconfig是自上而下检查的

更新:您应该在路由中指定控制器名称,否则就不会触发问题中的routeconfig,Default2。如果有两条路由以{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 }
     ); 
}

Routeconfig是自上而下检查的

更新:您应该在路由中指定控制器名称,否则就不会触发问题中的routeconfig,Default2。如果有两条路由以{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 }
     ); 
}

请您根据此修改并检查

我相信当url是

//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匹配,它将始终调用第一条路由。你们也在使用同样的模式来第二条路线。所以第二条路总是隐藏着。请检查并让我知道。

您能否根据此进行修改并检查

我相信当url是

//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匹配,它将始终调用第一条路由。你们也在使用同样的模式来第二条路线。所以第二条路总是隐藏着。请检查并让我知道。

@Html.ActionLink(“新建”、“创建”)
-这将生成一个url。鼠标悬停时,您可以在浏览器底部看到它。你能分享生成的url吗?@VeeKayBee我将鼠标悬停在ActionLink上,在底部显示:这就是它将索引的原因。它应该是
http://localhost://create
。提到的url将始终指向默认路由,即索引操作将始终被触发。但我已经定义了@Html.ActionLink(“新建”、“创建”)为什么它仍然指向索引?请尝试使用@Html.ActionLink(“电影”、“创建”)
@Html.ActionLink(“新建”、“创建”)
-这将生成一个url。鼠标悬停时,您可以在浏览器底部看到它。你能分享生成的url吗?@VeeKayBee我将鼠标悬停在ActionLink上,在底部显示:这就是它将索引的原因。它应该是
http://localhost://create
。提到的url将始终指向默认路由,即索引操作将始终被触发。但我定义了@Html.ActionLink(“创建新”、“创建”)为什么它仍然指向索引?尝试使用@Html.ActionLink(“电影”、“创建”)我将Default2移到顶部,现在它正在加载时调用创建操作。是否检查了其他操作。因为如果您这样指定,它将自动将所有路由发送到
Movies
cntroller和
Create
action。我尝试使用@Html.ActionLink(“编辑”、“编辑”)调用编辑操作,它正在更改URL,但仍然调用Index方法。尝试此操作:@Html.ActionLink(“新建”、“创建”、“电影”)@这是因为您需要修改每个路由的url模式。请尝试修改url模式好吗。我相信我们不能对多个路由使用相同的url模式。我在顶部移动了Default2,现在它正在加载时调用Create操作。您是否检查了其他操作。因为如果您这样指定,它将自动将所有路由发送到
Movies
cntroller和
Create
action。我尝试使用@Html.ActionLink(“编辑”、“编辑”)调用编辑操作,它正在更改URL,但仍然调用Index方法。尝试此操作:@Html.ActionLink(“新建”、“创建”、“电影”)@这是因为您需要修改每个路由的url模式。请尝试修改url模式好吗。我相信我们不能对多个路由使用相同的url模式。现在我修改了路由:routes.MapRoute(名称:“Default”,url:{Director}/{Movie}),默认值:new{controller=“Movies”,action=“Index”,id=urlparmeter.Optional,Director=urlparmeter.Optional,Movie=urlparmeter.Optional});MapRoute(名称:“Default2”,url:{action}),默认值:new{controller=“Movies”,action=“Create”,id=urlparmeter.Optional});还是不走运(你能检查一下我建议的方式吗?th有一些问题