本文介绍了ASP.Net的Web API的自定义操作把多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想实现在Web API控制器,​​采用多参数与ASP.Net MVC 4个Web API框架自定义操作。

 公共类APIRepositoryController:ApiController
{
    ...
    [HTTPGET,ActionName(retrieveObservationInfo)]
    公共ObservationInfo retrieveObservationInfo(
                    INT ID,
                    字符串名称1,
                    字符串名2)
    {
        //...做一点事...
        返回ObservationInfo;
    }
    ...
}

这样我可以调用一个URL在Web浏览器,如:

<$p$p><$c$c>\"http://[myserver]/mysite/api/APIRepository/retrieveObservationInfo?id=xxx&name1=xxx&name2=xxx\"

然而,这却从来没有工作过。

还有什么我需要配置,例如的WebAPI路由?目前我只使用默认 WebApiConfig.cs

在此先感谢


解决方案

在默认情况下的Web API将基于HTTP动词,而不是动作名派遣行动,你的情况:

  GET HTTP:// [MYSERVER] / mysite的/ API / APIRepository / ID = XXX和放大器;名1 = XXX和放大器;名称2 = XXX

要调度的基础上采取行动的名称(如你想要的),你需要的默认路由之前添加以下路径:

  routes.MapHttpRoute(
    名称:ActionApi
    routeTemplate:API / {控制器} / {行动} / {ID}
    默认:新{ID = RouteParameter.Optional}
);

请注意,您目前还无法(至少在没有黑客)在单个控制器相结合的动词为基础和行动的名称路由可靠。你可以阅读更多有关的的Web API路由。

I want to implement a custom action in a Web API controller that takes multiple arguments with ASP.Net MVC 4 Web API framework.

 public class APIRepositoryController : ApiController
{
    ...
    [HttpGet, ActionName("retrieveObservationInfo")]
    public ObservationInfo retrieveObservationInfo(
                    int id,
                    String name1,
                    String name2)
    {
        //...do something...
        return ObservationInfo;
    }
    ...
}

Such that I can call a URL in the web browser like:

"http://[myserver]/mysite/api/APIRepository/retrieveObservationInfo?id=xxx&name1=xxx&name2=xxx"

However, this has never worked.

Is there anything else I need to configure, e.g. WebAPI routing? Currently I just use the default WebApiConfig.cs.

Thanks in advance

解决方案

By default Web API will dispatch actions based on HTTP verb rather than action name, in your case:

GET http://[myserver]/mysite/api/APIRepository/?id=xxx&name1=xxx&name2=xxx

To dispatch based on action name (like you want), you need to add the following route before the default route:

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

Note that you currently cannot (at least not without hacks) combine verb-based and action-name routing in a single controller reliably. You can read more about Web API routing here.

这篇关于ASP.Net的Web API的自定义操作把多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:33