0

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

1 Answer 1

1

you should place your logic in a method and use it in 2 defferent Actions

[Route("SomeAction")]    
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult MyAction1()
{
    MyMethod()
    return View("MyAction");
}


[Route("~/OtherArea/some-route/SomeAction")]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult MyAction2()
{
    MyMethod();
    return View("MyAction");
}

private MyMethod(){
 ....
}

But they can still share the same View

Sign up to request clarification or add additional context in comments.

1 Comment

hmmm that's actually much simpler than I was expecting. Dunno how I didn't think of that >_<

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.