Parameters in the URL using /

2

I need to have a URL in the following format:

  

nomedosite.com/note/ {anything)

I need this url to fire the controller Note , with the action Index . How do I set up my route?

I tried to use this:

routes.MapRoute(
            name: "OpenNote",
            url: "{controller}/{*stringNote}",
            defaults: new { controller = "Note", action = "Index", stringNote = UrlParameter.Optional }
        );

But I always get page 404, that is, it's not working the way I want it to be. How do I set up my route?

    
asked by anonymous 30.05.2018 / 13:40

1 answer

1

When I need to mount routes with a parameter I just do it as follows:

Instead of leaving% dynamic% I already force the path of controller , I also add controller to accept only letters

routes.MapRoute(
           name: "OpenNote",
           url: "Note/{stringNote}",
           defaults: new { controller = "Note", action = "Index" },
           constraints: new { stringNote= @"[aA-zZ]" }
       );

A very important point is that custom routes should always come before the default route because it always "hits" the first route you can

routes.MapRoute(
           name: "OpenNote",
           url: "Note/{stringNote}",
           defaults: new { controller = "Note", action = "Index" },
           constraints: new { stringNote= @"[aA-zZ]" }
       );


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