简体   繁体   中英

The webpage cannot be found calling Web API Function

I have the following Web API defined

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MRNInput.Controllers
{
    [RoutePrefix("api/Appointment")]
    public class AppointmentController : ApiController
    {
        [AcceptVerbs("Post")]
        [Route("GetMissingKeys")]
        public IHttpActionResult GetMissingKeys([FromBody]string MRNList)
        {
            return Ok();
        }

        [AcceptVerbs("Get")]
        [Route("Get")]
        public IHttpActionResult Get()
        {
            return Ok();
        }

    }
}

The following is the Global.asax.cs file

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

namespace MRNInput
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }
}

WebApiConfig.cs is the following;

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

namespace MRNInput
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

I created the Get simply to test routing, Site starts at http://localhost:51520/Default.html - calling http://localhost:51520/api/Appointment/Get and I get The webpage cannot be found

.Net 4.5 application with Web API template used when creating a Web Application. Controller was added by using the Web API Controller - Empty template

Just replace [AcceptVerbs] attribute with [HttpGet], [HttpPost] etc. AcceptVerbs is the old attribute which is used in WebForms apps. Also if you calling your api from IE and return json it tries to download the result instead of showing it in browser

You could use [Route("")] to have access to GET api/Appointment request.

[HttpGet]
[Route("")]
public IHttpActionResult Get()
{
    return Ok();
}

Solved it. Still do not know the hows and why. Nothing wrong with the code as such.

Basically it was to do with the way the project was created. Selecting an empty project and clicking the Web API checkbox and you get the problem above. Instead select the Web API in the Select a template icons - this will select both the MVC and Web API checkboxes. I still had problems which required the solution to be cleaned but now it routes correctly.

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