-1

I have MVC project, I have added one API controller.in this API controller I have created methods in it.but when I am trying to call this API from postman or localhost with "http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs" Url its gives following response.

{
  "Message": "No HTTP resource was found that matches the request URI 'http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs'.",
  "MessageDetail": "No type was found that matches the controller named 'BedfordBrownstoneApi'."
}

My API controller is like following.

 public class BedfordBrownstoneApi : ApiController
    {
        // GET api/<controller>
        public int GetAgentId(string username,string password)
        {
            DataContext db = new DataContext();
            var data = db.Set<AgentLogin>().Where(a => a.UserName==username && a.Password==password).SingleOrDefault();
            return data.AgentId;
        }
}
}

My WebApiConfig class is like following.

  public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { action = "GetAgentId" }
            );
        }
    }
Victor Leontyev
  • 7,865
  • 2
  • 13
  • 34
Sanjiv Rajput
  • 105
  • 1
  • 11
  • try this - remove the 'Api' from controller name http://localhost:10133/api/BedfordBrownstone/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs – Asif Raza May 22 '17 at 05:06
  • Is there a `GlobalConfiguration.Configure(WebApiConfig.Register);` call inside your `Global.asax` file? – Alisson May 22 '17 at 05:10
  • 1
    You didn't mention Controller in your api controller name, check this http://stackoverflow.com/questions/11384552/getting-no-type-was-found-that-matches-the-controller-named-sampleslashbaseser – Venkateswaran R May 22 '17 at 05:10
  • 1
    Change your controller class name to 'BedfordBrownstoneApiController' – RK_Aus May 22 '17 at 05:19
  • @RK_Aus I did this change and call with "http://localhost:10133/api/BedfordBrownstoneApiController/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs" url but same issue – Sanjiv Rajput May 22 '17 at 05:23
  • @SanjivRajput - You have missed the call 'config.MapHttpAttributeRoutes();' in WebApiConfig also. I have updated the detail in my answer below. – RK_Aus May 22 '17 at 23:02

6 Answers6

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

add Id parameter in your routeTemplete in webapiconfig.

Curiousdev
  • 5,140
  • 3
  • 20
  • 36
Vishal Khatal
  • 99
  • 1
  • 9
0

Check Below Question that that similar problem as yours.

Check Accepted Answer of 'Benoit74B'. It clearly explains problem in details. Actual problem is regarding parameters you are passing -

"No HTTP resource was found that matches the request URI" here?

Community
  • 1
  • 1
Heemanshu Bhalla
  • 3,224
  • 1
  • 23
  • 45
0

Have you enabled attribute routing in api config? config.MapHttpAttributeRoutes();

With your current route, you should hit it via query string "http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs" and decorate the parameters with [FromUri]

   [HttpGet]
   [Route("api/BedfordBrownstoneApi/GetAgentId/")]
   public int GetAgentId(string username,string password)
   {  
      //api stuff
   }

or to hit the api via route parameters - http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId/yourusername/yourpassword

   [HttpGet]
   [Route("api/BedfordBrownstoneApi/GetAgentId/{username}/{password}")]
   public int GetAgentId(string username,string password)
   {  
      //api stuff
   }

Hope this helps you. Thanks!!

Prateek Gupta
  • 908
  • 1
  • 7
  • 20
0

There are two issues.

  1. Controller name - Change the name, add Controller in the name (BedfordBrownstoneApiController).
  2. You missed calling 'config.MapHttpAttributeRoutes()' in Register function.

Please update your 'Register' function in 'WebApiConfig' as below.

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { action = "GetAgentId" }
    );
}
RK_Aus
  • 756
  • 1
  • 8
  • 17
0

I have just change my code to following and now its work.

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "BedfordBrownstoneApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { action = "GetAgentId" }
    );
}
Sanjiv Rajput
  • 105
  • 1
  • 11