0

I have a Web API setup and I want to pass a string parameter to the GetAutomation method. In Global.asax I have:

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:

[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:

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. Any idea what I''m doing wrong? Thanks, J.

2 Answers 2

1
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

E : api/Automation/GetAutomation?testName=Test123

Sign up to request clarification or add additional context in comments.

1 Comment

Worked! Thanks for your help ElectroxRain. Just as an aside, if I have different methods such as RunAutomation1, RunAutomation2, etc., how could I pass parameters to those specific methods?
0

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.