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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jstuardo
  • 3,901
  • 14
  • 61
  • 136

5 Answers5

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.

pfx
  • 20,323
  • 43
  • 37
  • 57
  • 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`. – jstuardo Apr 19 '22 at 13:02
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 = "" }  
            );
MD. RAKIB HASAN
  • 3,670
  • 4
  • 22
  • 35
0

In the controller try using

[Route("MyFirst")]
[Route("MySecond")]
public class MyFirstController : Controller
{
  
}
Kvble
  • 286
  • 1
  • 8
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()

Sean McCafferty
  • 226
  • 2
  • 5
0

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

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

}
Rohan Rao
  • 2,505
  • 3
  • 19
  • 39