【问题标题】:The webpage cannot be found calling Web API Function找不到调用 Web API 函数的网页
【发布时间】:2015-11-04 00:28:42
【问题描述】:

我定义了以下 Web API

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();
        }

    }
}

以下是Global.asax.cs文件

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 如下;

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 }
            );
        }
    }
}

我创建 Get 只是为了测试路由,站点从 http://localhost:51520/Default.html 开始 - 调用 http://localhost:51520/api/Appointment/Get 并且我得到网页找不到

.Net 4.5 应用程序与创建 Web 应用程序时使用的 Web API 模板。控制器是使用 Web API 控制器添加的 - 空模板

【问题讨论】:

  • 您要的网址是什么?

标签: c# asp.net asp.net-web-api


【解决方案1】:

只需将[AcceptVerbs] 属性替换为[HttpGet], [HttpPost] 等。AcceptVerbs 是WebForms 应用程序中使用的旧属性。此外,如果您从 IE 调用您的 api 并返回 json,它会尝试下载结果而不是在浏览器中显示它

【讨论】:

  • 试过了 - 仍然给我网页找不到。使用 F12 并检查名为 localhost:51520/api/Appointment/Get 的地址
  • 在您的情况下,您不需要 Route("Get") 属性。如果方法名称与 HTTP 方法(Get、Post 等)相同,则无需设置 Route 属性。因为你只能放置HttpGet 属性并通过'localhost:51520/api/Appointment' url 调用你的方法。 Web API 与 MVC 非常相似,但又各有不同
  • 谢谢 - 也试过了。几乎就像没有设置路由一样
  • 如果你想使用AttributeRouting特性,你不应该为它做任何额外的配置。您只需在[Route] 属性中设置名称并使用此名称。在 web api 中,如果您将方法命名为 http 方法动词,则无需指定路由(例如 Get()、Post()、Put() Delete()),但如果方法名称与 htp 动词不同 - 您必须指定路由.您可以在不路由的情况下调用您的方法,只需在 MVC 应用程序中按名称调用您的方法,但它在 web api 中不起作用
  • 你可以阅读这篇关于如何在web api中使用属性路由asp.net/web-api/overview/web-api-routing-and-actions/…
【解决方案2】:

您可以使用[Route("")] 访问GET api/Appointment 请求。

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

【讨论】:

  • 谢谢 - 刚刚试过了,还是没什么区别。
【解决方案3】:

解决了。仍然不知道如何以及为什么。这样的代码没有错。

基本上这与项目的创建方式有关。选择一个空项目并单击 Web API 复选框,您就会遇到上述问题。而是在选择模板图标中选择 Web API - 这将同时选中 MVC 和 Web API 复选框。我仍然遇到需要清洁解决方案的问题,但现在它可以正确路由。

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2022-09-17
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多