简体   繁体   English

如何使用MVC4 Web API控制器调用两个以上的Get方法

[英]How to give a call to more than two Get method using mvc4 web api controller

I'm working on mvc4 web api project. 我正在研究mvc4 web api项目。

I have created a new controller GroupValuesController and i have following methods 我创建了一个新的控制器GroupValuesController ,我有以下方法

public GroupViewModel Get()
public List<GroupModel> Get(Guid Id)
public List<GroupModel> GetDetails(Guid Id)

First two methods are working fine I'm calling them from a group.cshtml view like following 前两种方法运行良好,我从group.cshtml view调用它们,如下所示

            $.getJSON(
                "api/groupvalues",
                function (data) {

and

$.getJSON(
                "api/groupvalues/" + id,
                function (data) {

For third controller method public List<GroupModel> GetDetails(Guid Id) i'm executing that from Details.cshtml view like following but it is not working. 对于第三个控制器方法, public List<GroupModel> GetDetails(Guid Id)我正在从Details.cshtml view执行,如下所示,但它不起作用。 i'm mismatching some calling ? 我打错了电话吗?

 function getGroupDataById(id, ctrl) {
        $.getJSON(
                "api/groupvalues/GetDetails/" + id,
                function (data) {

Is this related with Route? 这和路线有关吗?

 public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

Per the link below, in order to target that action method the way you have, you need to change your routing: 按照下面的链接,为了以您的方式定位该操作方法,您需要更改路由:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

http://www.codeproject.com/Articles/624180/Routing-Basics-in-ASP-NET-Web-API http://www.codeproject.com/Articles/624180/Routing-Basics-in-ASP-NET-Web-API

It really doesn't matter what you write after Get, it is going to call the first one with same arguments. 在Get之后编写的内容实际上并不重要,它将使用相同的参数调用第一个。 Web API don't rely on name rather they rely on HTTP verbs. Web API不依赖名称,而是依赖HTTP动词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM