问题标签 [webapi2]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
86 浏览

c# - 如何读取 [RoutePrefix("content")] 中的内容

可以在 C# web api 控制器中读取路由前缀值。我试过了,但我找不到任何方法来访问它。

[RoutePrefix("api/Account")]

如何将内部的值放入""变量中。

0 投票
1 回答
943 浏览

c# - 找不到方法:System.Web.Http.Results.UnauthorizedResult

我正在我的 Web API 2 应用程序中编写一些身份验证逻辑,并且在主题行中出现错误。起初我只是返回 Unauthorized() 但后来我在主题行中得到了错误。当我重新访问代码时,我注意到 Intellisense 没有显示 Unauthorized 的无参数构造函数,所以我不确定为什么它没有触发构建错误。看起来构造函数只接受 AuthenticationHeaderValues 的集合。因此,我更新了我的实现,只为这个参数传递了 null,但我仍然在主题行中收到错误。

我没有收到编译器或运行时错误,说参数丢失或需要,只是找不到方法,这看起来很奇怪。知道问题可能是什么吗?您能否提供一个基本的工作示例,其中包含我可以传递的参数,或者这似乎是一个不同的问题?

更新

我按照以下答案中的说明进行操作,但仍然遇到相同的错误。这是完整的堆栈跟踪:

0 投票
1 回答
848 浏览

c# - 使用 Windows 登录单点登录到 Web 应用程序

作为我们下一个版本的一部分,我有一个来自客户的单次签核要求。现有流程如下:

  1. 用户使用组织提供的电子邮件 ID 或用户名登录其组织系统(广告用户)或主门户系统。
  2. 用户点击链接到我的网站(即 angular + web api)
  3. 用户被自动视为登录用户,他可以访问受保护的页面。如果用户想从组织外部登录,他必须使用我们网站上的登录表单。

我已经使用我们存储在本地系统中的电子邮件/密码完成了登录表单 Web 应用程序,但我仍在努力使用窗口凭据来实现。

我的项目基于带有角度前端的 web api 2.0。它托管在 IIS 上。我不想使用任何第 3 方 DLL 来管理所有内容,那么 .NET 环境中是否有任何合适的解决方案可以以简单的方式满足我的要求?

0 投票
1 回答
76 浏览

structuremap - C# Web Api 2 use StructureMap to pass request data into injected dependency constructor parameter

I'm getting my feet wet with C# IoC frameworks. I chose StructureMap.Webapi2 to integrate into an existing api. I have the following scenario which I am not sure what the best way to implement is.

A peek a the WorkerService:

Approach 1:

  • Make WorkerService's constructor parameterless and add a public SomeData property that can be initialized inside MyController MyAction.
  • Then both ClientManager and WorkerService can be injected by the IoC into a constructor to be added to the controller.

The Action would then look like:

Approach 2 (the one I'm not sure how to implement)

  • Keep WorkerService constructor as is (with a parameter). Inject the service into the Controller's constructor (requires building and pass the service's argument (SomeData) at runtime, instead of having the MyAction build SomeData).
  • Somehow build SomeData (maybe using a factory) before for each request is handled by the controller. This would mean that ClientManager would have to be injected to that somehow/factory. The output of the somehow/factory would be used by the IoC when building the WorkerService to be injected into the controller, per request.

To me, Approach 1 seems quicker and simple, but Approach 2 seems to be more attractive, more challenging and with more learnings.

0 投票
1 回答
45 浏览

c# - 消费方法api c# put 2(String, Object)的参数

我正在尝试使用这种类型的 api:

这个想法来自一个控制台项目。

我找到了这段代码:

细节是它发送对象 p 但想法是发送对象 p 除了字符串 {id}

0 投票
0 回答
35 浏览

angular - 从 angular4 到 WebAPI2 的特殊字符“/”

我需要从 angular4 调用 web api2 我的 url 看起来像下面我的 url 看起来像 api/schedule/getdata/date/2018-04-18/utilityid/utilityname/poolid/pool/name 其中 poolid 包含特殊字符,例如:“pool /姓名”。我如何将这个特殊字符(/)从 angular4 传递到 WebAPI2。

我在 WebAPI 级别尝试使用 FromUri - 但仍然失败

HTTP 错误 404.0 - 未找到

您要查找的资源已被删除、名称已更改或暂时不可用

0 投票
1 回答
280 浏览

angular - 如何正确处理和控制 Angular 和 Web API 中的错误 - 2

我想知道如何正确处理和控制 Angular 和 Web api 中的错误,在一些教程中我看到错误是在组件中处理的,而不是在获取数据的服务中。基本上后端和角度/客户端错误都被处理在这样的组件中:

在此处输入图像描述

我想知道他怎么可能同时处理组件中的后端和前端错误?那么这是否意味着我不需要在 WEB API 中尝试/捕获?这也应该处理?

我在这里很困惑,请解释一下这个例子......

我认为错误处理程序应该添加到service.ts- 在这个例子PeopleService中,虽然PeopleService看起来像这样:

在此处输入图像描述

所以我对此感到困惑:

service.ts 中不应该有处理错误的逻辑吗?

我不应该在 WebApi 中编写 try/catch,这里如何处理后端错误?

感谢帮助伙计们干杯!

0 投票
1 回答
230 浏览

.net-core - 使用 UrlHelper 和新的 uri .net 核心构建链接时崩溃

我正在尝试在我的 webapi 的 .net 核心中使用 IUrlhelper 创建一个分页链接。

我得到错误

“值不能为空。参数名称:uriString”

我在启动时有以下代码:

然后我有一个类,我建立我的分页链接

如果执行以下操作,我的链接始终为空。不要明白!

//注意urlhelper在注入时不为null

0 投票
1 回答
259 浏览

c# - 带有附件的 C# MailMessage 会中断主题编码

我正在使用 System.Net.Mail.SmtpClient 发送电子邮件。问题是,当我向 MailMessage 添加附件时,收到的电子邮件主题和附件文件名解码出错。

当主题或附件名称字符串仅包含一个特殊字符(如“ü”)时,解码失败,但如果它包含更多类似“ÍŰÁÉÚŐÓÜÖíűáéúőóüö”,则解码正常。

重现问题的代码:

设置 message.SubjectEcoding 和其他似乎并不重要。默认值是 UTF-8,无论如何我都会使用它。

我正在使用 gmail smtp 进行测试,这是 webapi 2 项目的一部分。

0 投票
1 回答
733 浏览

c# - 使用 asp.net web api 2 将媒体上传到 azure blob 存储

我正在开发将图像上传到我的 azure blob 的 rest web api,我使用了其中一个在线教程并获得了此代码

它正在我的帐户 blob 容器中上传图像,但是当我打开 azure 存储管理器并访问容器时,我得到错误的格式,如下图所示

在此处输入图像描述

你能看到内容类型吗?我无法在资源管理器中打开此路径任何帮助将不胜感激