问题标签 [asp.net-web-api2]

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 回答
10894 浏览

c# - 带有 Web API 2 和 AngularJS 的 MVC5.1

我正在做一个辅助项目来自学 AngularJS 和 Web API,以及两者如何很好地协同工作。

我有很好的 ASP.NET MVC 知识,但我仍然无法理解 AngularJS 和 Web API 以及这三者如何协同工作。

目前,我有一个带有以下代码的 Web API 控制器:

在我Create.cshtml看来,我有ng-app指令,我创建了一个 JS 控制器并放置了ng-controller指令放在表单周围,并将其指向 JS 控制器。

但在这里我被困住了。首先,如何将我的ThingVM.csViewModel 绑定到 Angular?我需要JSONResult在我的 MVC 控制器上返回一个吗?如果是,如何?因为我尝试了以下,但它没有编译。

假设我让它工作,我如何将它绑定到 AngularJS,以便它知道我的 ViewModel 结构是什么样的?因为我的ThingVM有很多复杂程度。

最后,我如何处理表单提交,以便角度指向我的 Web API 控制器的POST请求。

0 投票
2 回答
321 浏览

web - 尽管设置了 Thread.CurrentPrincipal,但授权过滤器返回 401

我正在使用 HttpClient 和 HttpServer(内存中)运行集成测试。

当测试运行时,将执行令牌处理程序(消息处理程序),我添加此代码只是为了快速测试:

当我调试操作的控制器构造函数时,我会执行 base.User.Identity.IsAuthenticated 并将其设置为 TRUE。

我本来希望该操作会运行,因为 Thread.CurrentPrincipal 已设置。

为什么它不起作用?

0 投票
2 回答
8090 浏览

cookies - Request.GetOwinContext().Authentication.SignIn 不创建 cookie

以下代码未创建 .ASPNET Cookie,我在 WebAPI 自定义登录方法中使用此代码

0 投票
2 回答
28899 浏览

c# - Web API 2 使用异步任务下载文件

我需要编写如下方法来返回文本文档(.txt、pdf、.doc、.docx 等)虽然有很好的在 Web API 2.0 中发布文件的示例,但我找不到相关的只需下载一个。(我知道如何在 HttpResponseMessage 中做到这一点。)

以上内容是否需要异步?我只想返回流。(可以吗?)

更重要的是,在我最终以一种或另一种方式完成工作之前,我想知道做这种工作的“正确”方式是什么......(因此,非常感谢提及这一点的方法和技术)......谢谢。

0 投票
1 回答
2184 浏览

c# - 在 ASP.Net Web Api 2 中访问 Session 对象

如何使用Web Api 2控制器内部的Session功能?HttpContext

Session 对象为空,虽然我在 ASP.Net 的 Web Api 的第一个版本中看到了各种启用会话状态的方法,但这些方法似乎都不适用于 Web Api 2。

Web Api 参考 1: 使用 ASP.NET Web API 访问会话

我意识到这不是每个人都同意的方法,但是在我正在处理的应用程序中拥有一个状态完整的 API 会很有用。

0 投票
1 回答
1393 浏览

c# - 通过 Web API 2 将 ASP.NET 身份(用户管理)添加到 OData

我有一个从 Web API 模板(包括凭据)创建的 OData 项目。

我有一个 ApplicationUser:IdentityUser 类。

我有一个 TournamentContext : IdentityDbContext 类。

我有带有 [RoutePrefix("api/Account")] 属性的模板附带的默认 AccountController。

在 WebApiConfig.cs 对于 web api 模板默认路由我有

对于我添加的 OData 支持:

现在,我想通过 OData API 公开帐户/用户管理。我该如何做到这一点?

谢谢,贾尼夫拉特森。

0 投票
1 回答
2613 浏览

angularjs - Web Api 2 子域 Token 认证

我正在使用 AngularJS 和开箱即用的 WebApi2 令牌身份验证模板(个人用户帐户)创建一个站点。我试图让两个站点同时登录,一个在www.domain.com,另一个在sub.domain.com

目前,我在 Angular 中使用以下代码对用户进行身份验证:

并在之后为每个请求附加授权标头:

上面的代码允许每个站点单独登录,但是会话存储不会跨其他窗口/选项卡持续存在,因此它不会将用户登录到子域。

这篇博文中有一些关于这个问题的评论(半途而废):http ://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/

但是,实现起来似乎太复杂了(并且会产生用户被重定向的不良影响)。我希望像设置域一样简单,就像使用 cookie 一样:

我开始怀疑在当前情况下是否应该在 cookie 上使用令牌身份验证...

0 投票
1 回答
2953 浏览

c# - Query parameter route constraints

I just started using ASP.NET Web API 2.1 and have encountered a limitation. Using Attribute Routing, I can do the following:

The URL /item/5 will be routed to this action, but the URL /item/abc will not, because of the int constraint in {id:int}.

I tried changing my URL so that the id parameter was in the query string along with its constraint, despite the use of route constraints on query parameters never being mentioned or demonstrated in the documentation.

If I try to run now, I get an error on the Configure method call in Application_Start.

The message is as follows.

ArgumentException was unhandled by user code

The route template cannot start with a '/' or '~' character and it cannot contain a '?' character.

Two things bother me about this.

First, the section documenting Route Prefixes on MSDN makes clear that placing a ~ character at the start of the route template is completely acceptable. I tried it, and it works as documented.

Second, if not like this, how can I place a route constraint on a query parameter? Consider the following, with the route constraint removed.

The URL /item/5 will be routed to this action, with id set to 5 - but so will the URL /item/abc, with id set to 0.

Is there no way to place a route constraint on a query parameter?

0 投票
3 回答
38163 浏览

c# - Web api中多个过滤器的执行顺序

我正在使用最新web api的 .

我确实用 3 个不同的过滤器属性注释了一些控制器。

我不能确定过滤器是否按照它们从上到下声明的顺序运行。

如何定义执行顺序web api 2.1

https://aspnetwebstack.codeplex.com/workitem/1065#

http://aspnet.uservoice.com/forums/147201-asp-net-web-api/suggestions/3346720-execution-order-of-mvc4-webapi-action-filters

我还需要为自己解决这个问题吗??

0 投票
1 回答
3794 浏览

fiddler - Fiddler multipart/form-data with auth token asp.net web api 2

我已经为此苦苦挣扎了几天,(小提琴手新手)

我的网址如下所示:

现在这是一个多部分/表单数据内容类型,我尝试了一些方法来提交我的表单数据,如下所示:(虽然我提交文件没有问题..它只是表单数据和文件!)

在查询中的标记内容之后添加值

或者只是将它们添加到请求标头上但似乎没有任何效果,我仍然没有在控制器中获取我的表单数据值......

我认为这在提琴手中一定是相当明显和常见的事情,但为什么会遇到这么多麻烦呢?我错过了什么?

注意:我可以通过测试客户端使用 html 表单测试我的应用程序......enctype="multipart/form-data" method="POST">它可以工作......但不能在 Fiddler 中?