简体   繁体   English

如何设置Web API控制器以将请求路由到方法参数?

[英]How to setup Web API controller to route request to method parameter?

I have a Web API setup and I want to pass a string parameter to the GetAutomation method. 我有一个Web API设置,我想将字符串参数传递给GetAutomation方法。 In Global.asax I have: 在Global.asax中,我有:

protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{testName}",
            defaults: new { id = System.Web.Http.RouteParameter.Optional });
        RouteTable.Routes.MapHttpRoute(
            name: "DefaultApiWithAction",
            routeTemplate: "api/{controller}/{action}/{testName}");
    }

In my AutomationController.cs, I have: 在我的AutomationController.cs中,我有:

[ActionName("GetAutomation")]
    [HttpGet]
    public string StartAutomation(string testName)
    {
        //string testName = "MyTest123";
        Vmware.StartAutomation("automation-server", testName);
        return "Automation started for " + testName;
    }

If I remove the testName parameter from StartAutomation and call: 如果我从StartAutomation中删除testName参数并调用:

http://localhost/api/Automation/GetAutomation

it works. 有用。 If I put it back in and try 如果我放回去尝试

http://localhost/api/Automation/GetAutomation/Test123

it fails with a 404 error. 它失败并显示404错误。 Any idea what I''m doing wrong? 知道我在做什么错吗? Thanks, J. 谢谢,J。

Remove the {testName} from the "routeTemplate"

protected void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{testName}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional });
    RouteTable.Routes.MapHttpRoute(
        name: "DefaultApiWithAction",
        routeTemplate: "api/{controller}/{action}");
}

And include the parameter as a queryString 并包含参数作为queryString

E : api/Automation/GetAutomation?testName=Test123 E:api / Automation / GetAutomation?testName = Test123

This is regarding the question of the comment , if I understood properly the only thing you have to do is declare the input of the parameters on the function as 这是关于注释的问题,如果我正确理解的话,唯一要做的就是将函数上的参数输入声明为

[ActionName("RunMyStuff")]
 public string MyMethodName(bool isAtomic, string blabla, int delayOfSec)
{
    ....Code Placed Here
}

So the call will be like the other one , but now you have more than 1 parameters so you have to use the contact queryString parameters. 因此,调用将与另一个调用类似,但是现在您拥有多个参数,因此您必须使用contact queryString参数。

E : api/Automation/RunMyStuff?isAtomic=true&blabla=mystring&delayOfSec=23 E:api /自动化/ RunMyStuff?isAtomic = true&blabla = mystring&delayOfSec = 23

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

相关问题 如何为ZipFile设置Web API控制器 - How to setup web api controller for ZipFile Web API:如何在控制器操作没有相应参数时获取路由参数 - Web API: How to get a route parameter when controller action does not have corresponding argument .net web API:如何从代表 API 操作的控制器类方法中检索路由/路由模板? - .net web API: How to retrieve routes/route templates from controller class method which represents an API operation? 如何将空路由参数发送到 web API? - How to send empty route parameter to a web API? 在Web Api 2 Controller方法中访问路由和POST参数 - Accessing route and POST params in Web Api 2 Controller Method [controller]在Web API controller的route属性中是如何工作的? - How does [controller] work in route attribute of Web API controller? 如何使用Unity基于http请求参数动态地将服务注入到asp.net web api控制器中 - How to dynamically inject service into asp.net web api controller based on http request parameter using Unity Web API控制器按路线选择 - Web api controller selection by route 无法设置路由来调用控制器方法 - Unable to setup route to invoke controller method 我需要将参数从请求标头传递到API控制器操作方法。 我怎样才能? - I need to pass a parameter from request header to the API controller action method. How can I?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM