1

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 }
            );
        }
    }
}
Kemal AL GAZZAH
  • 967
  • 6
  • 15
  • Add `GlobalConfiguration.Configuration.MapHttpAttributeRoutes();` in your `Global.asax.cs` before you define your routes. – Rahul Sharma Feb 17 '22 at 17:37
  • Yes I added that, then I was getting an error , then I added after routing in glabal.asax.cs this instruction GlobalConfiguration.Configuration.EnsureInitialized();, and now its working, thank you for your help and have a nice time. – Kemal AL GAZZAH Feb 17 '22 at 17:45

1 Answers1

1

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();
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54