Tag: asp.net web api

container.RegisterWebApiControllers(GlobalConfiguration.Configuration)导致InvalidOperationException

在我的集成测试中,我使用的是我正在测试的Web API项目中构建的相同的SimpleInjector.Container。 但是这一行在组合根类中: container.RegisterWebApiControllers(GlobalConfiguration.Configuration); 导致exception: System.TypeInitializationException : The type initializer for ‘MyProject.Api.Test.Integration.HttpClientFactory’ threw an exception. —- System.InvalidOperationException : This method cannot be called during the application’s pre-start initialization phase. Result StackTrace: at MyProject.Api.Test.Integration.HttpClientFactory.Create() at MyProject.Api.Test.Integration.Controllers.ProductControllerIntegrationTest.d__0.MoveNext() in d:\Projects\My\MyProject.Api.Test.Integration\Controllers\ProductControllerIntegrationTest.cs:line 26 —– Inner Stack Trace —– at System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() at System.Web.Compilation.BuildManager.GetReferencedAssemblies() at System.Web.Http.WebHost.WebHostAssembliesResolver.System.Web.Http.Dispatcher.IAssembliesResolver.GetAssemblies() at System.Web.Http.Dispatcher.DefaultHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver) at System.Web.Http.WebHost.WebHostHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver) […]

将HttpRequestMessage转换为OwinRequest和OwinResponse转换为HttpResponseMessage

我有一个Web API消息处理程序MyHandler ,我想在OWIN管道中作为中间件运行。 所以像这样配置处理程序。 public class Startup { public void Configuration(IAppBuilder app) { app.UseHttpMessageHandler(new MyHandler()); HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( “DefaultWebApi”, “{controller}/{id}”, new { id = RouteParameter.Optional }); app.UseWebApi(config); } } 处理程序非常简单,什么都不做。 public class MyHandler : DelegatingHandler { protected override async Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { // <— breakpoint here var response […]

WebApi Route在Orchard模块中返回Not Found

我正在创建一个Orchard模块,我想添加一个WebApi控制器。 我的Module.txt: Name: ModuleName AntiForgery: enabled Author: The Orchard Team Website: http://orchardproject.net Version: 1.0 OrchardVersion: 1.0 Description: Description for the module Features: ModuleName: Description: Description for feature ModuleName. 我添加了ApiRoutes类: using Orchard.Mvc.Routes; using Orchard.WebApi.Routes; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; namespace ModuleName { public class ModuleNameApiRoutes : IHttpRouteProvider { public void GetRoutes(ICollection […]

Azure移动服务错误:在程序集中找到多个具有名称的静态类作为引导程序

从Visual Studio 2013部署新版本的C#MobileService项目后,Azure日志中报告了以下错误: 在程序集中找到了多个名为“WebApiConfig”的静态类作为引导程序:{{My.MobileService.Project,myMobileService}}。 请仅提供一个类或使用“IBootstrapper”属性来定义唯一的引导程序。 实际上,如果提到的类文件有多个版本,如何在部署新版本之前清除它?

使用Kentor.AuthServices.StubIdp作为生产IDP

我正在尝试在我的应用程序中实现IDP(SAML2)服务器。 鉴于我的应用程序具有所需的所有数据,我不希望我的任何合作伙伴要求我们的客户注册他们。 我对SAML2协议不太熟悉。 我发现项目Kentor.AuthServices.StubIdp是最有趣的,因为它实现了我需要的一切。 我也知道它不是为生产目的而建的。 我计划在StubIdp之上构建IDP,因为我买不起像ComponentPro这样昂贵的解决方案。 有更好的选择吗? 建立在StubIdp之上是一个好主意吗?

如何从WebAPI中的HttpResponse对象获取错误消息?

我有一个控制器,从以下代码生成exception,并带有以下消息: – public HttpResponseMessage PutABook(Book bookToSave) { return Request.CreateErrorResponse(HttpStatusCode.Forbidden, “No Permission”); } 我正在使用以下代码测试此方法: – var response = controller.PutABook(new Book()); Assert.That(response.StatusCode,Is.EqualTo(HttpStatusCode.Forbidden)); Assert.That(response.Content,Is.EqualTo(“No Permission”)); 但我得到一个错误,内容不是“无权限”。 似乎我无法将响应转换为HttpError以获取消息内容“No Permission”。 状态代码返回正常。 只是努力获取message content 。

Thread.CurrentPrincipal已经过身份validation,但ClaimsPrincipal.Current不是

我在我的WebApi项目中使用基于声明的授权,并且有一种方法可以检查当前身份是否经过身份validation。 当我使用ClaimsPrincipal.Current ,当前的身份未经过身份validation,但是当我使用Thread.CurrentPrincipal它就是。 ClaimsPrincipal.Current.Identity.IsAuthenticated; //False Thread.CurrentPrincipal.Identity.IsAuthenticated; //True 这看起来很奇怪,特别是因为MSDN说 ClaimsPrincipal.Current只返回Thread.CurrentPrincipal: 备注 默认情况下,返回Thread.CurrentPrincipal。 您可以通过设置ClaimsPrincipalSelector属性来指定要调用的委托来确定当前主体,从而更改此行为。 有人可以解释一下为什么ClaimsPrincipal未经过身份validation,而理论上两者都包含相同的身份吗?

WebApi中的C#await / async,重点是什么?

有人知道这样做的目的是什么? private async Task StoreAsync(TriviaAnswer answer) { … } [ResponseType(typeof(TriviaAnswer))] public async Task Post(TriviaAnswer answer) { var isCorrect = await StoreAsync(answer); return Ok(isCorrect); } 通过检查,它告诉它异步运行私有方法,但同步等待它结束。 我的问题是,这有什么意义吗? 或者这只是一种奇特而无用的技巧? 我在研究Web API / MVC / SPA的一些代码时遇到了这个问题。 无论如何,任何见解都会有用。

ASP.NET Web API – 请求特定的全局变量

当我收到Web API请求时,我想创建一个变量,在请求的生命周期中,每个类都可以访问该变量。 我想让它像任何类的App.User一样以静态属性的forms被访问。 但我不希望在处理请求后保留它,所以我猜SessionState不是一个选项。 这样做的正确方法是什么? 编辑:它也需要线程安全。

Web Api控制器无法识别Json方法

我有一个web api控制器 using sport.BLL.Abstract; using sport.BLL.Concrete; using sport.DAL.Entities; using sport.webApi.Models; using AutoMapper; using Microsoft.AspNet.Identity.EntityFramework; using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin.Security; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web; using System.Web.WebPages.Html; namespace sport.webApi.Controllers { public class AccountManageController : ApiController { [HttpPost] public System.Web.Mvc.ActionResult CreateAccount(CollaborateurModel item) { var user = new […]