c# - Angular 2,C# MVC ApiController : Error on Get Request from C# Rest Api

标签 c# rest angular typescript


我有 c# ApiController 的问题,我有一些像这样的:

public class Heroes
{
    public int userId;
    public int id;
    public string title;
    public string body;
}

public class HeroesController : ApiController
{
    // ...

    // GET: api/Heroes/5
    public Heroes Get(int id)
    {
        Heroes u = new Heroes() 
        { 
            userId = 1, title = "some titile", body = "some body" 
        };
        return u;
    }

    // ...
}

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

        config.Formatters.JsonFormatter.SupportedMediaTypes
            .Add(new MediaTypeHeaderValue("text/html"));

        config.Formatters.Remove(config.Formatters.XmlFormatter);
    }
}

Chrome 扩展 ReSTLet Client 的结果是正确的(200 ok)
请求“获取”在 http://localhost:63854/api/Heroes/5 上发送

{
"userId": 1,
"id": 0,
"title": "some titile",
"body": "some body"
}

但是,当我以 Angular 发送获取请求时,我收到一条错误消息

TypeError: WEBPACK_IMPORTED_MODULE_1_rxjs_Observable.Observable.throw is not a function


并且(以 Angular )如果我使用外部 rest api,有些像这样
https://jsonplaceholder.typicode.com/posts
它的工作正确,重新调整结果,我没有任何错误消息。

C# Controller 中 Rest Api 的错误在哪里?
我不怀疑错误是在 Angular 方面。

抱歉我的英语不好:c

最佳答案

尝试将此导入语句添加到客户端的顶部:

import 'rxjs/add/observable/throw';

关于c# - Angular 2,C# MVC ApiController : Error on Get Request from C# Rest Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44377081/

相关文章:

c# - 使用 C# 在 Selenium RC 中按 Enter 键

c# - WCF 服务,垂直记录

json - 创建基于 JSON 的 REST 完整 Web 服务 API 有哪些替代方案?

html - 删除出现在启动画面之前的白屏

使用 TypeScript 的联合类型时 Angular 模板检查错误

c# - 仅安装更新的框架时应用程序会运行吗

c# - C# 中的可空 DateTime

http - 为 Web API 驱动程序使用 HTTP 持久连接?

jQuery 删除(RESTful)

angular - 避免嵌套的可观察对象