【发布时间】: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:<portnmber>/<cntrollername>/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