Tag: asp.net web api

Web.API中的自定义授权

我对ASP.NET MVC的理解是,对于授权,我应该使用类似的东西 – public class IPAuthorize : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { //figure out if the ip is authorized //and return true or false } 但在Web API中,没有AuthorizeCore(..) 。 有OnAuthorization(..) ,MVC的一般建议是不使用OnAuthorization(..) 。 我应该在Web API中使用什么来进行自定义授权?

将自定义查询支持的导航属性添加到ODataConventionModelBuilder

情况 我创建了以下Model类 public class Car { public int Id {get;set;} public string Name {get;set;} public virtual ICollection PartStates {get;set; } } public class PartState { public int Id {get;set;} public string State {get;set;} public int CarId {get;set;} public virtual Car Car {get;set;} public int PartId {get;set;} public virtual Part Part {get;set;} } public class Part […]

如何在ASP.NET Web API中执行异步“即发即忘”操作

所以,我有这个Web API动作,它执行一个异步函数。 public void Post([FromBody]InsertRequest request) { InsertPostCommand.Execute(request); DoAsync(); } private async void DoAsync() { await Task.Run(() => { //do async stuff here }); } 我实际上希望控制器操作在执行异步操作时返回到客户端。 我这样做了,因此我得到一个例外: System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending. 我怎么能实现这个呢? 谢谢

web api put识别查询字符串但不识别正文

当我将用户作为查询字符串传递(使用$ http中的参数)并设置web api方法在uri中查找它们时,一切都很好。 但是当我按照下面的方式传递它们时,用户显示为null。 我在这里失踪了什么? 角度函数 scope.saveChanges = function () { // create array of user id’s var users = []; angular.forEach(scope.usersInRole, function (v, k) { users.push(v.Key); }); var data = { user: users }; var token = angular.element(“input[name=’__RequestVerificationToken’]”).val(); // put changes on server http({ url: config.root + ‘api/Roles/’ + scope.selectedRole + ‘/Users’, method: ‘PUT’, […]

对WCF服务的异步调用不会保留CurrentCulture

根据这个问题的答案async/await call应该保留CurrentCulture。 就我而言,当我调用自己的异步方法时,将保留CurrentCulture。 但是,如果我调用某种WCF服务方法,则不会保留CurrentCulture,而是将其更改为看起来像服务器默认线程文化的东西。 我查看了调用托管线程的内容。 它发生在每个代码行都在一个托管线程上执行(ManagedThreadId保持不变)。 在调用我自己的异步方法后,CultureInfo保持不变。 但是当我调用WCF的方法时,ManagedTrheadId保持不变,但CurrentCulture已更改。 简化的代码如下所示:: private async Task Test() { // Befoe this code Thread.CurrentThread.CurrentCulture is “ru-RU”, ie the default server’s culture // Here we change current thread’s culture to some desired culture, eg hu-HU Thread.CurrentThread.CurrentCulture = new CultureInfo(“hu-HU”); var intres = await GetIntAsync(); // after with await Thread.CurrentThread.CurrentCulture is still […]

在WebAPI中使用Model上的Serializable属性

我有以下场景:我正在使用WebAPI并根据模型将JSON结果返回给使用者。 我现在还需要将模型序列化为base64,以便能够将它们保存在缓存中和/或将它们用于审计目的。 问题是,当我将[Serializable]属性添加到模型以便将模型转换为Base64时,JSON输出更改如下: 该模型: [Serializable] public class ResortModel { public int ResortKey { get; set; } public string ResortName { get; set; } } 没有[Serializable]属性,JSON输出是: { “ResortKey”: 1, “ResortName”: “Resort A” } 使用[Serializable]属性,JSON输出为: { “k__BackingField”: 1, “k__BackingField”: “Resort A” } 如何在不更改JSON输出的情况下使用[Serializable]属性?

Autofac – 无法创建请求生命周期范围,因为HttpContext不可用 – 由于异步代码?

简短问题: 与此未解决的问题相同 长问题: 我刚刚从MVC 4 + Web Api解决方案中移植了一些代码,该解决方案使用Autofac进入我的新解决方案,该解决方案也使用Autofac但仅使用Web Api 2(没有MVC 5.1项目,只有web api)。 在我以前的解决方案中,我有MVC4和Web Api所以我有2个Bootstrapper.cs文件,每个文件一个。 我复制了新项目的Web Api引导程序。 现在我在新解决方案中有2个其他需要依赖项目的项目。 让我们假设我必须使用DependencyResolver.Current.GetService()尽管它是一个反模式。 起初,在我将MVC依赖关系解析器设置为同一容器之前,这是行不通的: GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); //I had to pull in Autofac.Mvc and Mvc 5.1 integration but this line fixed it DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 奇怪的是,这样做只会修复其中一个项目! 这是情况: Solution.Web project Bootstrapper.cs that registers both dependency resolvers for web api and mvc. […]

In-Memory HttpServer如何知道要托管哪个WebAPI项目?

我想使用流行的内存托管策略对WebAPI项目运行测试。 我的测试位于一个单独的项目中。 这是我考试的开始 [TestMethod] public void TestMethod1() { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: new {id = RouteParameter.Optional}); HttpServer server = new HttpServer(config); HttpMessageInvoker client = new HttpMessageInvoker(server) } 使用HttpServer初始化客户端,建立直接的客户端 – 服务器连接。 除了提供路由配置信息之外,HttpServer如何知道要托管哪个WebAPI项目? 如何同时托管多个WebAPI项目? 似乎HttpServer能够找到WebAPI项目吗? 谢谢

entity framework核心:此平台不支持类型Udt。 (空间数据 – 地理)

我正在尝试entity framework核心,偶然发现了一个我从未见过的错误,无法弄清楚如何修复它。 我正在使用.net Core Web API 2.0和EntityFramework Core 2.00-preview2-final 这是一个触发错误的简单示例。 (概念:从数据库中获取用户的简单端点) 错误:System.PlatformNotSupportedException:此平台不支持类型Udt。 有什么建议? 问题是我在我的数据库中使用地理,但我在模型中将它用作字符串,因为entity framework核心还不支持空间数据…… 如何在不摆脱地理位置的情况下保持这种蛋糕的美味,这是一个重要特征吗? 编辑:请参阅我当前解决方案的答案

使用JSON.Net解析ISO持续时间

我有一个Web API项目,其中包含Global.asax.cs的以下设置: var serializerSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Utc }; serializerSettings.Converters.Add(new IsoDateTimeConverter()); var jsonFormatter = new JsonMediaTypeFormatter { SerializerSettings = serializerSettings }; jsonFormatter.MediaTypeMappings.Add(GlobalConfiguration.Configuration.Formatters[0].MediaTypeMappings[0]); GlobalConfiguration.Configuration.Formatters[0] = jsonFormatter; WebApiConfig.Register(GlobalConfiguration.Configuration); 尽管如此,Json.Net无法解析ISO持续时间 。 它抛出此错误: 将值“2007-03-01T13:00:00Z / 2008-05-11T15:30:00Z”转换为“System.TimeSpan”时出错。 我正在使用Json.Net v4.5。 我尝试了不同的值,例如“P1M”和维基页面上列出的其他值,没有运气。 所以问题是: 我错过了什么吗? 或者我是否必须编写一些自定义格式化程序?