kiriti
kiriti

Reputation: 45

Multiple action routing error in webapi MVC4

I have write the below configutation code in the webapiconfig.cs file for routing the multiple fintion. In the get methods I am getting the Multiple actions were found that match the request: System.String GetJobDetails(System.String) error.

Webapiconfig.cs code

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

        config.Routes.MapHttpRoute(
            name: "RendererAPiStatus",
            routeTemplate: "shared/{controller}/{id}/status",
            defaults: new { action = "getJobStatus", id = RouteParameter.Optional }
        );

My controller code :

//post shared/rendererjob
    [HttpPost]
    public string createRendererJob(HttpRequestMessage request)
    {
        return "teststring";
    }

    //put shared/rendererjob/renderGUID
    [HttpPut]
    public string DoPutRequest([FromUri(Name="id")]string renderGUID)
    {
        return renderGUID;
    }

    //get shared/rendererjob/renderGUID
    [HttpGet]
    public string GetJobDetails([FromUri(Name = "id")]string renderGUID)
    {
        return renderGUID;
    }


    //get shared/rendererjob/renderGUID/status
   [HttpGet]
    public HttpResponseMessage getJobStatus([FromUri(Name = "id")]string jobid)
    {
        var response = Request.CreateResponse(HttpStatusCode.OK);
        string uri = Url.Link("RendererAPiStatus", new { id = jobid });
        response.Headers.Location = new Uri(uri);
        return response;
    }

the 3 URL are working fine, the //get shared/rendererjob/renderGUID[HttpGet] is not working and getting the multiple action error in the browser.

Any one please suggest me on this. Note : the Route method is not working in the MVC4 VS2012, and unable to instal any patches into my system for this.

Upvotes: 0

Views: 339

Answers (1)

Jon Susiak
Jon Susiak

Reputation: 4978

The problem is that you have two GET methods with the same signature, so you need to differentiate them somehow. Try adding a Default Action Name to your methods that use the RendererAPi route:

[HttpPost]
[ActionName("Default")]
public string createRendererJob(HttpRequestMessage request)
{
    return "teststring";
}

//put shared/rendererjob/renderGUID
[HttpPut]
[ActionName("Default")]
public string DoPutRequest(string id)
{
    return renderGUID;
}

//get shared/rendererjob/renderGUID
[HttpGet]
[ActionName("Default")]
public string GetJobDetails(string id)
{
    return renderGUID;
}

//get shared/rendererjob/renderGUID/status
[HttpGet]
public HttpResponseMessage getJobStatus(string id)
{
    var response = Request.CreateResponse(HttpStatusCode.OK);
    string uri = Url.Link("RendererAPiStatus", new { id = id });
    response.Headers.Location = new Uri(uri);
    return response;
}

Then change the RendererAPi route as follows:

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

By the way you don't need to include the [FromUri] attribute for strings.

Upvotes: 0

Related Questions