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?
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, ie 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" })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.