locked
No action was found on the controller 'User' that matches the request. RRS feed

  • Question

  • User-712926555 posted

    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

    Saturday, September 8, 2018 12:55 PM

Answers

  • User2103319870 posted

    // 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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 8, 2018 1:42 PM