javascript Web API - 无法加载资源:服务器响应状态为 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23245283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:36:52  来源:igfitidea点击:

Web API - Failed to load resource: the server responded with a status of 404

javascriptangularjsasp.net-web-api

提问by Lichte

I am developing angular js with web api.

我正在使用 web api 开发 angular js。

I have this cotroller: Controller/MasterController, and thiis in my WebApi Config:

我有这个控制器:Controller/MasterController,这在我的 WebApi 配置中:

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

I call this function from Global.asax, Application_Start event.

我从 Global.asax、Application_Start 事件调用这个函数。

I call my web api from service.js like that:

我从 service.js 这样调用我的 web api:

var service = function ($http) {
    var _$http = $http;
    self = this;
    self.getMenuItems = function () {
        var promise = _$http({
            method: "GET",
            url: 'api/Master'
        }).success(function (data, status, headers, config) {

        }).error(function (data, status, headers, config) {

        });
        return promise;
    };

In debug mode, I saw that I reach this area. I get this error in chrome console: "Failed to load resource: the server responded with a status of 404"

在调试模式下,我看到我到达了这个区域。我在 Chrome 控制台中收到此错误: “加载资源失败:服务器响应状态为 404”

and this is what he was trying to get to: http://localhost:12345/api/Master

这就是他想要达到的目标: http://localhost:12345/api/Master

Also, I tried to get to the web api controller directly through the browser, and I couldn't find it.

另外,我试图通过浏览器直接访问web api控制器,我找不到它。

Thank You

谢谢

采纳答案by Lichte

After trying some new directions, I received new error details. The final error I received + its solution you can find here

在尝试了一些新方向后,我收到了新的错误详细信息。我收到的最终错误 + 你可以在这里找到它的解决方案

回答by h3li0s

Just to be sure your API is working try the following:

为了确保您的 API 正常工作,请尝试以下操作:

Global.asax.cs:

Global.asax.cs:

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

WebApiConfig:

WebApiConfig:

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

MasterController:

主控制器:

using System.Web.Http;

namespace WebApi.Controllers
{
    public class MasterController : ApiController
    {
        public string Get()
        {
            return "Hello world";
        }
   }
}

Calling http:// localhost:[SomePort]/api/Master in your browser should result in this: "Hello world"

在浏览器中调用 http:// localhost:[SomePort]/api/Master 应该会得到这样的结果:“Hello world”

The configurations above are all standard when creating a new WebApi.

以上配置都是新建WebApi时的标准配置。