1

I have two types of controller in my application. One WebApi controller and one MVC controller. With the following structure of my project:

Project
|-- Controllers
|   |-- Api
|   |   |-- AccountController
|   |   |-- ...
|   |-- AccountController
|   |-- ...

As you can see, I have AccountController twice in two different namespaces though. The one in the folder Api is the WebApi controller and the one under Controllers is a MVC controller.

I would like to use @Url.Action("GetAll", "Account") inside my view to generate a link to my WebApi controller. How do I differentiate between the Api controller and the MVC controller. How can I tell it whether to use the Api or MVC controller?

0

1 Answer 1

3

In a word: you can't. Url.Action has no capability to distinguish by namespace; you'll need unique controller names. This is part of the reason why it's typical to add Api in the an API controller's name, i.e. AccountController and AccountApiController.

If you really want to use AccountController for both, your only real option is to use areas. You can create an Api area, rather than just a subfolder under Controllers, and then you can do:

@Url.Action("GetAll", "Account", new { area = "Api" })
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your response Chris! I saw that it was able to distinguish only if the Action that I call is unique in both controllers. However, when the same Action is specified in both Controllers, the selection of the appropriate Controller is kind of random. I guess that I will have to add the Api keyword in my Controller names then.

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.