【问题标题】:ASP.NET MVC different routing for different actionsASP.NET MVC 不同的路由不同的动作
【发布时间】:2013-06-18 05:50:05
【问题描述】:

我正在使用 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

Index 操作运行良好,但即使我尝试使用 @Html.ActionLink("Create New", "Create") 调用 Create 操作,它仍会调用 Index 操作。根据我目前的理解,它应该调用CreateAction。如果我的问题看起来很愚蠢,我是 MVC 的新手,很抱歉。但是,我在这里错过了什么重要的事情?

【问题讨论】:

  • @Html.ActionLink("Create New", "Create") - 这将生成一个 url。用鼠标悬停时,您可以在浏览器底部看到它。你能分享一下生成的网址吗?
  • @VeeKayBee 我将鼠标悬停在 ActionLink 上,底部显示:localhost:#myportnumber
  • 这就是它要编入索引的原因。它应该像http://localhost:&lt;portnmber&gt;/&lt;cntrollername&gt;/create。提到的 url 将始终指向默认路由,即 index 操作将始终被触发。
  • 但是我已经定义了@Html.ActionLink("Create New", "Create") 为什么它仍然指向索引?
  • 试试@Html.ActionLink("Movies","Create")

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


【解决方案1】:

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 }
     ); 
}

【讨论】:

  • 我将 Default2 移到顶部,现在它在加载时调用 Create Action。
  • 您是否检查了其他操作。因为如果你这样指定,它会自动将所有路由发送到Movies cntroller 和Create action。
  • 我尝试使用 @Html.ActionLink("Edit", "Edit") 调用 Edit 操作,它正在更改 URL localhost:port/Movies/Edit 但仍在调用 Index 方法。
  • 试试这个动作:@Html.ActionLink("Create New", "Create", "Movies")
  • @UsmanKhalid 那是因为您需要修改每条路由的 url 模式。您能否尝试修改 url 模式。我相信我们不能为多条路线使用相同的 url 模式。
【解决方案2】:

能否请您按此修改并检查。

我相信当 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 匹配,它将始终调用第一条路由。而且您也在第二条路线上使用相同的模式。所以第二条路线总是隐藏的。请检查并告诉我。

【讨论】:

  • 现在我修改了路由: routes.MapRoute( name: "Default", url: "{Director}/{Movie}", defaults: new { controller = "Movies", action = "Index ", id = UrlParameter.Optional, Director = UrlParameter.Optional, Movie = UrlParameter.Optional } ); routes.MapRoute( name: "Default2", url: "{action}", defaults: new { controller = "Movies", action = "Create", id = UrlParameter.Optional } );仍然没有运气。 :(
  • 您能否检查一下我建议的方式,您提到的网址存在一些问题。您能否复制我在代码中显示的 url 模式。请尝试使用相同的模式
猜你喜欢
  • 2018-03-11
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多