Suman Manna
Suman Manna

Reputation: 23

The resource cannot be found when invoke ApiController method

When i try to invoke api controller the getting 404. But when i try to invoke Controller method its working fine. Please help and let me know if any information is required.

TPServicesAPIController.cs:-

using AgentVartualOffice.Models.LogIn;
using System.Web.Mvc;
using AgentVartualOffice.Models;
using Newtonsoft.Json;
using System.Text;

namespace AgentVartualOffice.Controllers.TPServices
{
    public class TPServicesAPIController : ApiController
    {
 public string Myauth()
        {

            return "True";

        }
}
}


Global.asax.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;


namespace AgentVartualOffice
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {


            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            GlobalFilters.Filters.Add(new HandleErrorAttribute());

        }
    }
}

Invoke url:
http://localhost:61868/TPServicesAPI/Myauth

Upvotes: 1

Views: 185

Answers (1)

Nkosi
Nkosi

Reputation: 246998

Chances are your are calling the wrong URL.

The default Web API convention-based routes are usually prefixed with api

public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}", //<<--- default web API route template
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

and also do not use action names unless the route template has been changed.

routeTemplate: "api/{controller}/{action}/{id}"

So I suggest you check your API config and update the URL being called accordingly.

For example, by calling: api/TPServicesAPI/Myaut

Upvotes: 1

Related Questions