0

My application is a very simple MVC 5 website with several controllers. I have a Home controller with an Index action like this:

public ActionResult Index(string token)
{
   DoSomethingWithTheToken(token);
   return View();
}

Now, supossing my website is www.my-special-website.com I'd want www.my-own-purpose-website.com/abcdefghijklmno to be served by the Index method.

According to the information available in https://msdn.microsoft.com/en-us/library/cc668201.aspx I set up this routes:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

Unfortunately, when I navigate to www.my-special-website.com/abcdefghijklmno, I get a "404 resource not found" response.

Who can I make it land to the desired controller and pass the token parameter?

Yván Ecarri
  • 1,661
  • 18
  • 39
  • 1
    shouldn't the `URL` be as `{controller}/{action}/{token}` .....? – vikscool Aug 16 '18 at 08:59
  • You get an error because that url navigates to a controller method named `abcdefghijklmnoController` which does not exist. If you want to omit the controller and action names from the url, then you need a route constraint (refer [Routing in ASP.NET MVC, showing username in URL](https://stackoverflow.com/questions/37358416/routing-in-asp-net-mvc-showing-username-in-url/37359345#37359345) for an example –  Aug 16 '18 at 09:03
  • If you want "Index" to be included in URL then and only then add that in MapRoute otherwise omit the action parameter from that.. (same for the controller) – Dhiren Aug 16 '18 at 09:04

1 Answers1

1

I think you are getting the 404 error for the defined route is because you have defined the routes of your application as:

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

And you can make it work as:

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name:"myroute",
            url:"{token}",
            defaults: new { controller = "Home", action = "MyIndex", token = "{token}" }
            );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
}

Note 1: The order of the routes that you add to the route table is important. Our new custom route should be added before the existing Default route. If you reversed the order, then the Default route always will get called instead of the custom route.

Note 2: If the route is defined as url:{token} will cause a problem as all the incoming route which is just the controller name ex: www.mywebsite.com/Home here the Home will be treated as the token. So, to handle this kind of situations most people will use the filters such as Authentication filters with or without Identity Server. Here is a basic implementation of how to add an authentication filter.

vikscool
  • 1,293
  • 1
  • 10
  • 24
  • That means you could never navigate to say the `Index` method of `ProductController` using `../Product` –  Aug 16 '18 at 09:25
  • well, yes that will depend on the route that is created, that's why in the comment of the question I have suggested the `url` to be as `{controller}/{action}/{token}` instead of just `{token}`. – vikscool Aug 16 '18 at 09:28
  • Except that is not what OP has asked for in the question :) –  Aug 16 '18 at 09:30