0

I have checked links related to mvc5, webapi2 but not being able to figure out my mistake.

My Problem: /api/EBanking/CheckLogin is not excuting code of checkLogin method in ebankingcontroller

Links checked:

Custom Routing not working in MVC5

WebAPI2 and MVC5 route config

QueryString with MVC 5 AttributeRouting in Web API 2

App_start Code:

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AutoMapperCentralAppConfig.Configure();
    }

RouteConfig.cs

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

WebApiConfig.cs

public static string UrlPrefixRelative { get { return "~/api"; } }
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

APi Controller:

    [RoutePrefix("api/EBanking")]
public class EBankingController : ApiController
{
    public EBankingController()
    {
    //some other code, it runs
    }

    [HttpGet, HttpPost]
    [Route("CheckLogin")]
    public IEnumerable<usr06user_role> CheckLogin(string UserName, string Password)
    {
    //main code which doesn;t runs
    }

    public IEnumerable<usr06user_role> GetAll()
    {
    //test code which runs when we call: /api/ebanking/
    }

result screenshot: enter image description here

KoolKabin
  • 17,157
  • 35
  • 107
  • 145

1 Answers1

2

In your WebApiConfig.cs add another route for action based routing like this:

    // Web API routes
    config.MapHttpAttributeRoutes();

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

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

This "api/{controller}/{action}/{id}" will allow calls to api/ebanking/checklogin

Alternatively you can even add full route in attribute such as:

[Route("api/EBanking/CheckLogin")]
public IEnumerable<usr06user_role> CheckLogin(string UserName, string Password)
{
    //main code which doesn;t runs
}
degant
  • 4,861
  • 1
  • 17
  • 29
  • tried both suggested method, added extra routemap but still not working... adding screenshot of my result screen – KoolKabin May 02 '17 at 09:34
  • Which routes are working? Does `api/ebanking` work? Do you have any other routes that work? – degant May 02 '17 at 09:36
  • I have a suggestion. Try to go step by step.. first get rid of the routeprefix/route from controller level. Add a new httpget with a different routeprefix. Your first objective should be do get 2 HttpGet's working in the web api controller and the above code should help you out. By default Web Api Controllers let you have one httpget, one httppost and so on. Once you can get a simple one working you should be able to move on to CheckLogin with parameters. – degant May 02 '17 at 10:37
  • i figured out that missing parameter names username and password fields in querysting was causing problem... – KoolKabin May 04 '17 at 10:55