Venkat
Venkat

Reputation: 2186

routeconfig.cs - page doesnot exist

This is my routeconfig.cs

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("Background/AutoComplete.aspx/{*webmethod}");
        routes.IgnoreRoute("Background/Misc.aspx");

        routes.MapRoute(
          name: "NoAuthorName",
          url: "Author",
          defaults: new { controller = "Author", action = "Index" });

        routes.MapRoute(
           name: "AuthorNameonly",
           url: "{controller}/{author}",
           defaults: new { controller = "Author", action = "AllQuotesByAuthorWithoutAuthorID" });

        routes.MapRoute(
            name: "AuthorNamewithAuthorID",
            url: "{controller}/{author}/{id}",
            defaults: new { controller = "Author", action = "AllQuotesByAuthorWithAuthorID"});

        routes.MapRoute(
          name: "AllQuotesByAuthorWithAuthorIDandQuoteID",
          url: "{controller}/{author}/{id}/{id1}",
          defaults: new { controller = "Author", action = "AllQuotesByAuthorWithAuthorIDandQuoteID" });

        routes.MapRoute(
        name: "FinalURLwithAuthor",
        url: "{controller}/{author}/{id}/{id1}/{quotesseparatedbyhyphen}",
        defaults: new { controller = "Author", action = "Finalurl" });


        routes.MapRoute(
         name: "NoKeywordName",
         url: "Keyword",
         defaults: new { controller = "Keyword", action = "Index" });

        routes.MapRoute(
          name: "AllQuotesByKeywordwithoutKeywordID",
          url: "{controller}/{key}",
          defaults: new { controller = "Keyword", action = "AllQuotesByKeywordwithoutKeywordID" });

        routes.MapRoute(
           name: "AllQuotesByKeywordWithKeywordID",
           url: "{controller}/{key}/{id}",
           defaults: new { controller = "Keyword", action = "AllQuotesByKeywordWithKeywordID" });


        routes.MapRoute(
         name: "Default",
         url: "{controller}/{action}/{id}",
         defaults: new { id = UrlParameter.Optional });

This is how it works. http://sitename.com/Author/Benjamin-Quotes/AuthorID - works.

http://sitename.com/Author/Benjamin-Quotes/ - works.

http://sitename.com/Author - works.

but

http://sitename.com/Keyword - works

http://sitename.com/Keyword/Love - doesnot work 404. error

http://sitename.com/Keyword/Love/Keywordid - doesnot work. 404. error

However when i remove all map routes for author and have only for keyword like this in routeconfig.cs

  routes.MapRoute(
         name: "NoKeywordName",
         url: "Keyword",
         defaults: new { controller = "Keyword", action = "Index" });

        routes.MapRoute(
          name: "AllQuotesByKeywordwithoutKeywordID",
          url: "{controller}/{key}",
          defaults: new { controller = "Keyword", action = "AllQuotesByKeywordwithoutKeywordID" });

        routes.MapRoute(
           name: "AllQuotesByKeywordWithKeywordID",
           url: "{controller}/{key}/{id}",
           defaults: new { controller = "Keyword", action = "AllQuotesByKeywordWithKeywordID" });


        routes.MapRoute(
         name: "Default",
         url: "{controller}/{action}/{id}",
         defaults: new { id = UrlParameter.Optional });

now http://sitename.com/Keyword - works

http://sitename.com/Keyword/Love - works

http://sitename.com/Keyword/Love/Keywordid - works.

Now if I mix both author route and keyword route in routeconfig.cs the following url doesnot exist.HTTP 404. error

http://sitename.com/Keyword/Love -

http://sitename.com/Keyword/Love/Keywordid - .

Upvotes: 0

Views: 131

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239430

Your route patterns for both AllQuotesByKeywordwithoutKeywordID and AllQuotesByKeywordWithKeywordID are the same as those for AuthorNameonly and AuthorNamewithAuthorID, respectively. The fact that you're naming the route parameters differently is not enough to distinguish the routes from each other. As a result, the first one that matches wins, which in this case is AuthorNameonly and AuthorNamewithAuthorID. In other words, when you hit a route like /Keyword/Love, Keyword goes into controller, giving you the correct controller, but, Love is put into an author param, which your action on KeywordController will not be able to utilize, because it's looking for a key param. Further, since you're not specifying the action, the default action is being utilized, which for this route is AllQuotesByAuthorWithoutAuthorID, which is not even an action on KeywordController, hence your 404.

Long and short, you need something else to distinguish these routes from each other. You could potentially just change the routes to:

routes.MapRoute(
    name: "AuthorNameonly",
    url: "author/{author}",
    defaults: new { controller = "Author", action = "AllQuotesByAuthorWithoutAuthorID" });

routes.MapRoute(
    name: "AuthorNamewithAuthorID",
    url: "author/{author}/{id}",
    defaults: new { controller = "Author", action = "AllQuotesByAuthorWithAuthorID"});

And

routes.MapRoute(
    name: "AllQuotesByKeywordwithoutKeywordID",
    url: "keyword/{key}",
    defaults: new { controller = "Keyword", action = "AllQuotesByKeywordwithoutKeywordID" });

routes.MapRoute(
    name: "AllQuotesByKeywordWithKeywordID",
    url: "keyword/{key}/{id}",
    defaults: new { controller = "Keyword", action = "AllQuotesByKeywordWithKeywordID" });

In other words, instead of letting the controller param be variable, you specify it explicitly. Now, each route will only match if that first part of the URL path matches, removing the ambiguity.

Upvotes: 3

Related Questions