3

I am new in using web api and I am trying to call a specific method in my controller.

I have

global.asax

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

the WebApiConfig class with these routings

 // Web API routes
 config.MapHttpAttributeRoutes();


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



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

and my controller

[HttpGet]
public HttpResponseMessage GetPatSummary(string PatId)
{
   PatientSummary Pat = new PatientSummary();

   HttpResponseMessage Response = new HttpResponseMessage();
   string yourJson = Pat.GetPatient(PatId);
   Response = this.Request.CreateResponse(HttpStatusCode.OK, yourJson);
   return Response;
}

[ActionName("DefaultAction")] //Map Action and you can name your method with any text
public IHttpActionResult GetPatient(int id)
{
    Object Obj = new object();

    if (Obj!=null)
    {
       return NotFound();
    }
    return Ok(Obj);
}

the URL I am using is

http://localhost/mdmwapi/api/MdmwPatientController/GetPatSummary/sgdgdgddhdhd1334254

but I get this error A path segment cannot contain two consecutive parameters. They must be separated by a '/' or by a literal string.

I am getting nut :-)

5
  • what is mdmwapi portion of url ? Commented Oct 5, 2016 at 10:11
  • is the project name Commented Oct 5, 2016 at 10:37
  • you should remove this part and try again what happen, because its not the part of url Commented Oct 5, 2016 at 10:38
  • I did and ai get a 404 error as the url in IIS is registered to be localhost/mdmwapi I can try to re-create the virtual directory to be localhost/api Commented Oct 5, 2016 at 10:43
  • So I recreate the virtual directory to localhost/api so that the full URL is now localhost/api/MdmwPatientController/GetPatSummary/… but I get the same error Additional information: A path segment cannot contain two consecutive parameters. They must be separated by a '/' or by a literal string. Commented Oct 5, 2016 at 10:47

2 Answers 2

6

Use attribute routing

[HttpGet]
[Route("api/MdmwPatientController/GetPatSummary/{PatId}")]
public HttpResponseMessage GetPatSummary(string PatId)
{
    PatientSummary Pat = new PatientSummary();

    HttpResponseMessage Response = new HttpResponseMessage();
    string yourJson = Pat.GetPatient(PatId);
    Response = this.Request.CreateResponse(HttpStatusCode.OK, yourJson);
    return Response;
}

then you can request it using

http://localhost/api/MdmwPatientController/GetPatSummary/yourpatid

also you can map any url using attribute routing this way

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

2 Comments

Ok so can I delete the routing from my WebApiConfig?
keep defaults and remove others
0

the solution is a combination of a new route and a mistake in the URL

the new routes are now these ones

// Web API routes
            config.MapHttpAttributeRoutes();   

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


            config.Routes.MapHttpRoute(
                name: "ApiMethodCall",
                routeTemplate: "api/{controller}/{action}/{PatId}",
                defaults: new
                {
                    controller= "MdmwPatient",
                    action= "GetPatSummary"    
                    }
                );

and the error in the URL was that although the controller class name is MdmwPatientController I have to omit the "controller" suffix when calling from the test client, so the correct url is

http://localhost/mdmwapi/api/MdmwPatient/GetPatSummary/sgdgdgddhdhd1334254

1 Comment

I want to thank Mostafiz because his suggestions lead me to figure it out my solution and for this reason I am accepting his answer

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.