Reputation: 3110
I am building a Site using ASP.NET MVC. In RouteConfig
, I modified the method like this:
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 }
);
}
And in IndexView, I coded like:
@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>
Actually I modified RouteConfig
because I want different URLs for different directors and different movies in order to satisfy our client's SEO requirement
.
It is working fine for the Index
Action but even when I tried to invoke Create
action using @Html.ActionLink("Create New", "Create")
, it still invokes Index
action. According to my understanding so far, it should invoke Create
Action. I am new to MVC so sorry if my question seems foolish. But what major thing, I am missing here?
Upvotes: 1
Views: 1829
Reputation: 12437
Routeconfig is checked top down.
Update: You should specify the controller name in the route otherwise the way your routeconfig was in your question, Default2 would never have been fired.Having 2 routes start with {something} means the second one won't be fired. I'm guessing you want your URL to be localhost/movies/create/id
. For that, do this:
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 }
);
}
Upvotes: 4
Reputation: 15630
Can you please modify as per this and check.
I believe when the url is
//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 }
);
}
I am not 100% sure, you need some modification. But you cannot pass a similar type of url pattern and invoke two controllers.
As per the code in your question it will invokes always the first route, if the url matches. And you are using same pattern to second route also. SO second route always hidden. Please check and let me know.
Upvotes: 0