How to create ASP.NET Web API Url?

前端 未结 4 830
無奈伤痛
無奈伤痛 2020-11-29 16:59

In ASP.NET MVC, we have @Url.Action for actions. Is there something similar like @Url.Api which would route to /api/controller?

4条回答
  •  醉酒成梦
    2020-11-29 17:40

    Here is the KISS method for answering the question:

    If this is the code that you would use to create a MVC controller URL

    @Url.Action("Edit", "MyController")
    

    In order to get a URL for the API version of the controller (assuming you use the same controller name) you can use

    @Url.Action("Edit", "api/MyController")
    

    All the Url.Action method is doing is appending the root path of the application, with the controller name, followed by the action name (unless it is "Index" in which case it is not appended. if the route values object has an id property the value is also appended to the URL.

提交回复
热议问题