0

I am in the need to use routes to API controller methods as prefixes for redirects.

I (of course) could hardcode them but I would prefer to get them dynamically so if I change the routing or controller names it gets updated by the logic.

  [Route("api/v1/[controller]")]
  public class TestController : ControllerBase
  {
    [HttpGet("{path}")]
    public async Task<IActionResult> Get(string path)
    {
      string prefix = "api/v1/Other/"; //how to get that part dynamically?
      return Redirect(prefix + path);
    }
  }

  [Route("api/v1/[controller]")]
  public class OtherController : ControllerBase
  {
    [HttpGet("{path}")]
    public async Task<IActionResult> Get(string path)
    {
    }
  }

How would I get the prefix in TestController as string dynamically updating if the Route annotation or the controller name changes? I really need it at string.

1
  • if controller name can be changed, why not change this code too? Commented Jul 31, 2018 at 15:07

2 Answers 2

1

You can try to get the Route Data of the current request, change the name of the controller and generate an url with the updated new Route Data :

var routeData = this.HttpContext.GetRouteData();
routeData.Values["controller"] = "Other";
string url = Url.RouteUrl(routeData.Values);
return Redirect(url);
Sign up to request clarification or add additional context in comments.

Comments

0

You can return a RedirectToActionResult instead of a RedirectResult to redirect to a specific controller action by calling eg RedirectToAction, eg :

return RedirectToAction("Other","OtherAction");

Or use RedirectToRoute :

return RedirectToRoute(new {action="OtherAction",controller="Other"}); 

Generating the URL

The URL Generation section in the Routing to controller actions in ASP.NET Core article shows how to use URL.Action to generate the absolute link to another controller's action. From the example :

var url = Url.Action("Buy", "Products", new { id = 17, color = "red" });
return Content(url);

Adapting this to the question :

var url = Url.Action("OtherAction", "Other");
return Redirect(url);

Url is an instance of IUrlHelper

4 Comments

Thanks for the input, it definitly would work like that in that scenario. But I really need it as string since I am populating response models holding those URLs. Thats why I can't use the RedirectMethods of the controllers.
In this case you can use one of the IUrlHelper methods to generate the link, as shown in URL Generation, eg var url=Url.Action("OtherAction", "Other");
@PanagiotisKanavos monty did not tell full story, his another request is here: stackoverflow.com/questions/51615005/…
Well, the full story is a complex controller doing a couple of things and i have two different problems there. But SO users/moderaters use to remind me too not ask two questions in one post.

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.