0

On my home controller all but one action has no ID parameter, for all of the actions without an ID parameter I would like the action but not the controller to show in the URL. For one action I would like neither the controller or action to show in the URL, only the ID parameter for that action, e.g.:

routes.MapRoute(
            "OnlyCourse",
            "{courseabbrev}",
            defaults: new { controller = "Home", action = "course", courseabbrev = UrlParameter.Optional }
        );

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

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

However if I put the OnlyCourse route first the index action routes through it and if I put the OnlyAction route first the OnlyCourse route is not found. How can I get these routes to respond to specific actions independently?

edit -

So for example the URL for Only Course (course action) would be:

http://www.sitename.come/courseabbrev

and the URL for the other actions would be

http://www.sitename.com/action

I've tried the following now:

routes.MapRoute(
            "OnlyCourse",
            "{courseabbrev}",
            defaults: new { controller = "Home", action = "course"},
            constraints: new { action = "course" }

        );

        routes.MapRoute(
            "OnlyAction",
            "{action}",
            defaults: new { controller = "Home" }
        );

And this does the trick for the index action, however any other action still uses the OnlyCourse route. I tried adding a constraint but it doesn't seem to do much as that route will consider all actions aside from Index it seems.

Rob
  • 199
  • 21
  • can you add URL of both OnlyCourse and OnlyAction from which you are trying to access – Usman Apr 24 '17 at 11:36
  • Hello, I've added the URL's in question plus some other findings. Cheers. – Rob Apr 24 '17 at 11:39
  • you dont need action only the default route will handle for you even if there is no id – Usman Apr 24 '17 at 11:50
  • Correction to my previous comment, the default will indeed handle it but either way I need a separate route because I don't want the action name to show in the URL for the action 'course'. – Rob Apr 24 '17 at 11:54
  • 1
    it wont work like that because both routes are alike – Usman Apr 24 '17 at 12:02
  • This is a common misconfiguration. See [Why map special routes first before common routes in asp.net mvc?](http://stackoverflow.com/a/35674633/181087) for an explanation why it is misconfigured and possible solutions. – NightOwl888 Apr 25 '17 at 23:45

1 Answers1

0

it wont work because template of both routes "{courseabbrev}" and "{action}" are alike means if you use "{courseabbrev}" it will ignore "{action}" so you have to make one of them to be more specific like this

routes.MapRoute(
    "OnlyCourse",
    "courseabbrev/{courseabbrev}",
    defaults: new { controller = "Home", action = "course"}

);

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

so your URL will be

http://www.sitename.come/courseabbrev/yourcourseabbrev

http://www.sitename.com/action

if you want route to be /courseabbrev then

 routes.MapRoute(
  "OnlyCourse",
  "{courseabbrev}",
   defaults: new { controller = "Home", action = "course" }
   );

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

now your URL will be

http://www.sitename.come/courseabbrev

http://www.sitename.com/controller/action

Usman
  • 4,615
  • 2
  • 17
  • 33
  • Hello, thanks for your answer, yes I'm aware I can differentiate using a different action name but that doesn't solve my problem. The entire point of my question is that I would like the URL to only be 'courseabbrev' for the OnlyCourse action without anything before it. So unfortunately your solution does not resolve the issue. If it is simply not possible using this type of routing I will look into attribute routing perhaps. – Rob Apr 24 '17 at 12:31
  • @Rob then you dont need action only route let me edit my answer – Usman Apr 24 '17 at 12:41
  • Hello Usman, sorry perhaps I haven't been clear, I need URL's as per my original question. So for all actions except course the URL would be sitename.com/action and for course it would be sitename.com/courseabbrev. So I need specific routing for all actions except course and for course itself. I have achieved this using attribute marking but if there is a way to do it using this traditional routing that would be useful too. Cheers – Rob Apr 24 '17 at 13:55