简体   繁体   English

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

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

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 Config中有这个:

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 谢谢

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: 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时,以上配置均为标准配置。

After trying some new directions, I received new error details. 尝试了一些新的指导后,我收到了新的错误详细信息。 The final error I received + its solution you can find here 我收到的最终错误及其解决方案可以在这里找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载资源失败:服务器响应状态为 404(未找到)GoogleBooks Api - Failed to load resource: the server responded with a status of 404 (Not Found) GoogleBooks Api 加载资源失败:服务器响应状态为 404() - Failed to load resource: the server responded with a status of 404 () 加载资源失败:服务器响应状态为404 - Failed to load resource: the server responded with a status of 404 无法加载资源:服务器响应状态为404? 资源问题 - Failed to load resource: the server responded with a status of 404 ? Resource issue 程序无法加载无法加载资源:服务器以404(未找到)状态响应 - Program will not load Failed to load resource: the server responded with a status of 404 (Not Found) Node.js服务器:无法加载资源:服务器以状态404(未找到)响应 - Nodejs server: Failed to load resource: the server responded with a status of 404 (Not Found) 加载资源失败:服务器响应状态为 404(未找到)? - Failed to load resource: the server responded with a status of 404 (Not Found)? 加载资源失败:服务器响应状态为404(未找到) - getting Failed to load resource: the server responded with a status of 404 (Not Found) 加载资源失败:服务器响应状态为404() SpringBoot - Failed to load resource: the server responded with a status of 404 () SpringBoot 加载资源失败:服务器响应状态为404()Controller.js:1 - Failed to load resource: the server responded with a status of 404 () Controller.js:1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM