简体   繁体   中英

Unable to reach ASP.NET MVC Web Api endpoint

I am working on an ASP.NET MVC app. I am trying to create a basic API. I created my first Web API controller by right-clicking on Controllers, Add -> Controller... then choosing "Web API 2 Controller - Empty". In the controller code, I have the following:

namespace MyProject.Controllers
{
    public class MyApiController : ApiController
    {
        public IHttpActionResult Get()
        {
            var results = new[]
            {
                new { ResultId = 1, ResultName = "Bill" },
                new { ResultId = 2, ResultName = "Ted" }
            };
            return Ok(results);
        }
    }
}

When I run the app, I enter http://localhost:61549/api/myApi in the browser's address bar. Unfortunately, I get a 404. I'm just trying to create an API endpoint that returns a hard-coded set of JSON objects. I need this to test some client-side JavaScript. What am I doing wrong?

Here are how my routes are registered: WebApiConfig.cs

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

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  );
}

You did not add method name at the end of call. Try this one:

http://localhost:61549/api/myapi/get

Make sure that you have the WebApiConfig registration being called, possibly in the Global.asax Application_Start() method. Something like:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Try this approach

namespace MyProject.Controllers
{
        public class MyApiController : ApiController
        {
            public IHttpActionResult Get()
            {
                var results = new List<ResultModel>
                {
                    new ResultModel() {ResultId = 1, ResultName = "Bill"},
                    new ResultModel() {ResultId = 2, ResultName = "Ted"}
                };
                return Ok(results);
            }

        }

        public class ResultModel
        {
            public int  ResultId { get; set; }
            public string ResultName { get; set; }
        }
}


Api: http://localhost:61549/api/MyApi/get

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM