0

I am developing a web api but it can not hit it. Error shows 404 not Found.

Web Api

using Atea.Azure.ApiMangement.Business;
using System.Web.Http;

namespace Azure_API_Delegation_Portal.Controllers
{
    [RoutePrefix("api/apim")]
    public class ApimController : ApiController
    {
        private readonly ISubscriptionService _subscriptionService;
        [HttpGet]
        [Route("{string:productId}")]
        public bool GetProductSubscribe(string productId)
        {
            return _subscriptionService.IsSubscribed(productId);
        }
    }
}

How I call an API https://localhost:44300/api/apim/ldkjfk232

Web API Route

using System.Web.Http;

namespace Azure_API_Delegation_Portal
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

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

Image

enter image description here

2 Answers 2

1

I am missing this line of code in Application_Start() function in "Global.asax" file.

GlobalConfiguration.Configure(WebApiConfig.Register);
Sign up to request clarification or add additional context in comments.

Comments

1

Fix your route template. It is string by default so no need for the string constraint

//GET api/apim/ldkjfk232"
[HttpGet]
[Route("{productId}")]
public bool GetProductSubscribe(string productId)

Also note that the constraint goes after the placeholder name like this example

[Route("{paramaterName:int}")]

Read more about attribute routing here : Attribute Routing in ASP.NET Web API 2

It will show you how to properly configure your web api.

1 Comment

error remains the same after your suggested answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.