Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 }
            );
        }
    }
}

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading