To link to Controller/A/<anId>
I do this:
@Html.ActionLink(anId, "Action", "Controller", new {id = anId})
Action is underlined by Resharper and I can navigate to it with F12.
But I have a link to an api controller:
@Html.ActionLink("API Version", "../api/controller/", new {id = anId})
This does not have a resharper navigation option and is not going to refactored if I rename the controller. Is there a cleaner way to link to an APIController from a razor view? Particularly one Resharper recognises.
They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data. ApiControllers are specialized in returning data.
It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder. The name of a controller class must end with "Controller" and it must be derived from System.
Example: public class ValuesController : ApiController { // GET /api/values public IEnumerable<string> Get() { // returns /api/values/123 string url = Url.
As other answers have suggested already, you can use the Url.RouteUrl
like so:
@Url.RouteUrl("DefaultApi", new {httproute= "", controller = "MyApiController"});
You can also use the shortcut method Url.HttpRouteUrl
instead like so:
@Url.HttpRouteUrl("DefaultApi", new { controller = "MyApiController" })
"DefaultApi"
is the default Route name when creating a new WebApi project. This string should match whatever name you gave your HttpRoute in the WebApiConfig.cs
file.
Both of these solutions will only help you generate the URL to the route, they won't actually generate an HTML link for you. You need to use them like this:
<a href="@Url.HttpRouteUrl("DefaultApi", new { controller = "MyApiController" })">My Link Text</a>
If you want Razor to generate the HTML anchor tag for you, you can use this method instead:
@Html.RouteLink("My Link Text", "DefaultApi", new { controller = "MyApiController" })
This method provides a very similar interface to @Html.ActionLink
which is used for regular Controller/Action links.
All of these solutions allow click-through to work with ReSharper.
You actually have to update the api routes, because api by default does not include an action.
So add this route:
config.Routes.MapHttpRoute(
name: "ActionRoute",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
You can then create a link like so:
@Url.RouteUrl("ActionRoute", new {httproute= "", controller = "controller", action = "action"});
EDIT
I re-read your question and it doesn't say anything about an action, so I just assumed that, either way, you can still use @Url.RouterUrl
:
@Url.RouteUrl("DefaultApi", new {httproute= "", controller = "controller"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With