Last post Sep 08, 2018 01:42 PM by A2H
Member
138 Points
357 Posts
Sep 08, 2018 12:55 PM|Hisanth|LINK
Hi,
This is my first Project in web api. I am creating web api for User Login
In my web api 2 Controller like as follows
using MyServices.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace MyServices.Controllers { public class UserController : ApiController { [Route("api/User")] [HttpGet] public HttpResponseMessage AppLogin(String userName, String passWord) { Admin_AppUserBLL objusr = Admin_AppUserBLL.GetAdmin_AppUserBLL(userName); if (objusr != null) { if (objusr.Password == passWord) { objusr.Message = "valid username"; } else { objusr = new Admin_AppUserBLL(); objusr.Message = "username or password is invalid"; objusr.Status = 0; } } else { objusr = new Admin_AppUserBLL(); objusr.Message = "username or password is invalid"; objusr.Status = 0; } //return objusr.ToString();// HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK, objusr); return res; } } }
While consuming this service
http://localhost:50007/api/User/AppLogin?userName=123&passWord=123
It shows error like No action was found on the controller 'User' that matches the request.
In my webApiConfig
public static void Register(HttpConfiguration config) { // Web API configuration and services
// Web API routes config.MapHttpAttributeRoutes();
// config.Routes.MapHttpRoute( // name: "DefaultApi", // routeTemplate: "api/{controller}/{id}", // defaults: new { id = RouteParameter.Optional } //);
I Have tried like as follows, but still same issue
// config.Routes.MapHttpRoute( // name: "AppLogin", // routeTemplate: "api/{User}/{name}/{password}", // defaults: new { name = RouteParameter.Optional, password = RouteParameter.Optional } // ); }
How to solved it
All-Star
50841 Points
9895 Posts
Sep 08, 2018 01:42 PM|A2H|LINK
Hisanth // routeTemplate: "api/{User}/{name}/{password}",
You have mismatch in parameter name. Change your Route configuration like below
public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); //Customized ROute config.Routes.MapHttpRoute( name: "AppLogin", routeTemplate: "api/{controller}/{userName}/{passWord}", defaults: new { name = RouteParameter.Optional, password = RouteParameter.Optional } ); //Default Route config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
After this change you can access your webapi method like below
http://localhost:8089/api/User?userName=YourUserNameValue&passWord=YourPasswordValue
Member
138 Points
357 Posts
No action was found on the controller 'User' that matches the request.
Sep 08, 2018 12:55 PM|Hisanth|LINK
Hi,
This is my first Project in web api. I am creating web api for User Login
In my web api 2 Controller like as follows
While consuming this service
http://localhost:50007/api/User/AppLogin?userName=123&passWord=123
It shows error like No action was found on the controller 'User' that matches the request.
In my webApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
// config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
I Have tried like as follows, but still same issue
// config.Routes.MapHttpRoute(
// name: "AppLogin",
// routeTemplate: "api/{User}/{name}/{password}",
// defaults: new { name = RouteParameter.Optional, password = RouteParameter.Optional }
// );
}
How to solved it
All-Star
50841 Points
9895 Posts
Re: No action was found on the controller 'User' that matches the request.
Sep 08, 2018 01:42 PM|A2H|LINK
You have mismatch in parameter name. Change your Route configuration like below
public static void Register(HttpConfiguration config) { // Web API routes config.MapHttpAttributeRoutes(); //Customized ROute config.Routes.MapHttpRoute( name: "AppLogin", routeTemplate: "api/{controller}/{userName}/{passWord}", defaults: new { name = RouteParameter.Optional, password = RouteParameter.Optional } ); //Default Route config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
After this change you can access your webapi method like below
Aje
My Blog | Dotnet Funda