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

1.2k Views Asked by At

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?

5

There are 5 best solutions below

1
pfx On BEST ANSWER

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.

0
MD. RAKIB HASAN On

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 = "" }  
            );
0
Kvble On

In the controller try using

[Route("MyFirst")]
[Route("MySecond")]
public class MyFirstController : Controller
{
  
}
0
Sean McCafferty On

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()

0
ABDELLATIF LAGHJAJ On

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

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

}