1

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?

6
  • @Html.ActionLink("Create New", "Create") - this will generates a url. While hovering with the mouse, you can see it at the bottom of the browser. Can you please share the url generated ? Commented Jun 18, 2013 at 5:52
  • @VeeKayBee I hover over the ActionLink and at bottom it showed : localhost:#myportnumber Commented Jun 18, 2013 at 5:54
  • that is the reason it is going to index. It supposed to be like http://localhost:<portnmber>/<cntrollername>/create. The mentioned url will always point to default route that is index action will be fired always. Commented Jun 18, 2013 at 5:57
  • But I have defined @Html.ActionLink("Create New", "Create") why it is still pointing to index? Commented Jun 18, 2013 at 5:58
  • try with @Html.ActionLink("Movies","Create") Commented Jun 18, 2013 at 6:03

2 Answers 2

4

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 }
     ); 
}
Sign up to request clarification or add additional context in comments.

8 Comments

I moved Default2 at top, Now it is invoking Create Action on load.
Did you checked the other action. Because if you specify like this it will automatcally send all the routes to Movies cntroller and Create action.
I tried to invoke Edit action using @Html.ActionLink("Edit", "Edit"), it is changing the URL localhost:port/Movies/Edit but still invoking Index method.
try this action: @Html.ActionLink("Create New", "Create", "Movies")
@UsmanKhalid that is because you need to modify the url pattern for each route. Can you please try modifying the url pattern . I believe we cannot have same url pattern for multiple routes.
|
0

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.

2 Comments

Now I modified the Route : 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 } ); Still no luck. :(
Can you please check the way I suggested, there are some issues with the url you mentioned. Can you pls copy the url pattern as I shown in my code. Please try using the same pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.