Kemal AL GAZZAH
Kemal AL GAZZAH

Reputation: 1047

Web API 2 method not found

I have an existing WEB Form(Framework 4.7.2) app to which I added a Web API called ClaimController

I tried to call the webpi using this url /api/claim/Getclaims/2022 but it was not found.

any thing is missing ?

ClaimController.cs

using System.Net.Http;
using System.Web.Http;
using Dashboard.App_code;

namespace Dashboard.api
{
    public class ClaimController : ApiController
    {    
        
        
        [System.Web.Http.Route("api/Claim/Getclaims/{year?}")]
        [System.Web.Http.HttpGet]
        public IHttpActionResult Getclaims(string year)
        {
            return Ok(ClData.GetClaimsChart(year));
        }
       
    }
}

Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http;
using System.Web.Routing;

namespace Dashboard
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            
            RouteTable.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = System.Web.Http.RouteParameter.Optional }
            );
        }
    }
}

Upvotes: 1

Views: 283

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 8311

Regarding your setup, you need to add GlobalConfiguration.Configuration.MapHttpAttributeRoutes() before your define your routes in Global.asax.cs file:

GlobalConfiguration.Configuration.MapHttpAttributeRoutes();

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

GlobalConfiguration.Configuration.EnsureInitialized();

Upvotes: 1

Related Questions