I am trying to setup a single controller to use 2 different URLs.
So what I want is to navigate to:
- mysite.com/MyArea/some-route/SomeAction
- mysite.com/OtherArea/some-route/SomeAction
and have them both go to the same place.
So I have a class set up like this:
[RouteArea("MyArea", AreaPrefix = "MyArea")]
[RoutePrefix("some-route")]
[Route("{action}")]
public class MyController : Controller
{
[Route("SomeAction")]
[Route("~/OtherArea/some-route/SomeAction")]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult MyAction()
{
return View();
}
}
So this works - though looks a little messy.
I can type in either URL into the browser and it will pick up this action/page.
The 'OtherArea' doesn't really exist. It's just sometimes I want to use the first url, sometimes the second.
1) How do I route to this action and specify the url?
RedirectToAction("MyAction", "MyController", new { area = "MyArea" });
I'm only specifying the controller/action - the URL it finds by itself. Can I force it to use one or the other?
Ideally without hardcoding the path.
2) Is there a better way of doing what I am trying to do?
I'm not a fan of this line:
[Route("~/OtherArea/some-route/SomeAction")]
But I also don't want to create a copy of the controller and a new Area just for the sake of having a second url.
Thanks