ASP.NET MVC中针对不同动作的不同路由
本文关键字:路由 NET MVC ASP | 更新日期: 2023-09-27 18:18:45
我正在使用ASP建立一个网站。净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
行动,但即使当我试图调用Create
行动使用@Html.ActionLink("Create New", "Create")
,它仍然调用Index
行动。根据我目前的理解,它应该调用Create
Action。我是新的MVC所以抱歉,如果我的问题似乎很愚蠢。但是我在这里漏掉了什么重要的东西呢?
路由配置是自顶向下检查的。
Update:你应该在路由中指定控制器名,否则就像你的问题中的routecconfig一样,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 }
);
}
我不是百分之百确定,你需要一些修改。但是你不能传递类似类型的url模式并调用两个控制器。
根据你问题中的代码,如果url匹配,它将始终调用第一个路由。你对第二条路线也使用同样的模式。所以第二条路线总是隐藏的。