How to set 2 routes point to the same controller in ASP.NET MVC 5
Asked Answered
S

5

9

I have an ASP.NET MVC 5 web site which has a controller MyFirstController.

That way, I can load the index view by using https://example.server.com/MyFirst.

On the other hand, I have another view that renders the link (HREF attribute of HTML A tag) this way:

Url.Action("Index", "MyFirst")

That URL rendering causes the link to be generated:

https://example.server.com/MyFirst 

So far, so good. Now, I need to have a route something like this

https://example.server.com/MySecond

That URL should load the same Index view of MyFirst controller without creating a new controller named MySecondController. This is like having a MyFirst controller alias named MySecond.

In RouteConfig.cs file I added this:

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

The problem I have with this code is that I have this rendering code in the other view:

@if (somecondition)
{
    ... Url.Action("Index", "MyFirst") ...
}
else
{
    ... Url.Action("Index", "MySecond") ...
}

I need that if somecondition is true, the rendered URL to be https://example.server.com/MyFirst and if it is false, the rendered URL to be https://example.server.com/MySecond.

The problem I am having is that both are being rendered as https://example.server.com/MySecond.

How can I accomplish this task?

Shaikh answered 7/4, 2022 at 19:49 Comment(1)
Did you try adding another entry with for maproute with different name like for "MyFirst"Riker
R
4

Instead of using Url.Action, use Url.Route as it allows to explictly pass the name of the route to apply for rendering the url.

To render the /MySecond url, pass the name of the MySecond route.

@Url.RouteUrl("MySecond")

Do the same to render the /MyFirst url, but since it doesn't have an explicit route name, you'll have to pass both the name of the default route and the name of the MyFirst controller.

@Url.RouteUrl("Default", new { Controller = "MyFirst" })

You can omit the name of the Index action, as it has been set up as the default one.

Rameau answered 12/4, 2022 at 14:38 Comment(1)
Many thanks... it worked. Just one comment. I needed to use actually @Url.RouteUrl("MySecond", new { Action = "Index" }) because this link is inside a partial view which replicates on all pages (this is a navigation menu). When loaded page is a third controller with certain action, for example, https://example.server.com/MyThird/TheAction. the actual rendered link for MySecond route was https://example.server.com/MySecond/TheAction. When using explicit action in Url.RouteUrl, the rendered link was the right one: https://example.server.com/MySecond.Shaikh
A
0

Custom route should be before default route.

routes.MapRoute(
                "MySecond",                                          
                "MySecond/{action}/{id}",                            
                new { controller = "MyFirst", action = "Index",id = UrlParameter.Optional  }
            );

routes.MapRoute(
                "Default",                                              
                "{controller}/{action}/{id}",                           
                new { controller = "Home", action = "Index", id = "" }  
            );
Angelika answered 15/4, 2022 at 9:26 Comment(0)
C
0

In the controller try using

[Route("MyFirst")]
[Route("MySecond")]
public class MyFirstController : Controller
{
  
}
Coinstantaneous answered 15/4, 2022 at 13:4 Comment(0)
V
0

Instead of using Url.Action use Url.Route. This method has the option to provide the route name and optionally the action and controller.

if (somecondition)
{
    @Url.RouteUrl("Default", new { Controller = "MyFirst" }) //Action "Index" will be defaulted so do not need to provide it
}
else
{
    Url.RouteUrl("MySecond");
}

You could also create extension methods on UrlHelper so that you don't have "magic strings" littered through out your views:

namespace MyWebsite.Helpers
{
    public static class UrlHelperExtension
    {
        public static string MyFirst(this UrlHelper url)
        {
            return url.RouteUrl("Default", new { Controller = "MyFirst" });
        }

        public static string MySecond(this UrlHelper url)
        {
            return url.RouteUrl("MySecond");
        }
    }
}

Then you can use @Url.MyFirst() or @Url.MySecond()

Vomit answered 19/4, 2022 at 11:5 Comment(0)
M
0

You can set 2 route point to the same controller like shown below:

[Route("first_route")]
[Route("second_route")]
public class MyController : Controller{

}
Mylor answered 19/4, 2022 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.