Tag: asp.net web api

除非Fiddler正在运行,为什么IE11会创建空白的post请求?

我的AngularJS $ http发布对我的C#WebAPI restful服务的请求在Internet Explorer 11的Windows 8.1上失败。Firefox和Chrome都可以正常工作。 更多细节: IT部门说我们的网络没有代理 所有浏览器都不会选中所有“自动检测”和“使用代理”设置 请求在我的localhost上无法访问IIS,并在本地服务器上运行站点和服务 IE11的增强保护模式已关闭 请求的连接头是’keep-alive’(我也尝试’关闭’,它仍然失败) 有时一个请求会成功,只有第二个请求才会失败 没有显示错误 – IE的网络选项卡中的请求只显示’待定’,所有标题和正文都是空白的 我使用的是HTTP,而不是HTTPS 我已经尝试了元标记’X-UA-Compatible’IE9和Edge 在他们的机器上使用IE11的同事的请求也失败了 当Fiddler运行时,所有浏览器中的所有调用都能正常工作 Visual Studio 2013浏览器链接已关闭(因此SignalRArtery JS文件不会不断调用服务器并干扰测试) 我的AngularJS请求调用如下所示: var url = UrlService.GetUrlOfApi() + ‘Account/SignIn’; var postData = { ‘username’ : username, ‘password’ : password }; $http( { ‘url’: url, ‘data’: postData, ‘method’: ‘POST’ }) .success(function (data) { […]

OData中实体的别名/重命名属性

使用ODataConventionModelBuilder及其EntitySetfunction,是否可以重命名实体集上的属性名称? 假设我有一个实体集类型, Foo 。 它有两个属性, Bar和Baz 。 但是,在我的OData模型中,我希望这些属性分别命名为Jack和Jane 。 我可以这样做吗? 我希望这样的事情: var builder = new ODataConventionModelBuilder { Namespace = “Blah” }; var foo = builder.EntitySet(“Foo”); foo.AliasProperty(f => f.Bar, “Jack”); foo.AliasProperty(f => f.Baz, “Jane”); 到目前为止,我一直无法找到这样做的东西。

从自托管的WEB API控制台应用程序返回jsonp

我使用了这篇博客文章中描述的jsonp格式化程序: http : //www.west-wind.com/weblog/posts/2012/Apr/02/Creating-a-JSONP-Formatter-for-ASPNET-Web-API 有没有人尝试将格式化程序与自托管控制台应用程序一起使用? 我在常规的MVC 4项目中尝试过格式化程序,它立即起作用。 但是,我想在自托管控制台应用程序中使用它,我在使用它时遇到了很多麻烦。 我已注册格式化程序,并validation它已添加: var config = new HttpSelfHostConfiguration(serviceUrl); config.Formatters.Insert(0, new JsonpMediaTypeFormatter()); 我已经validation在使用以下代码发出请求时正在调用格式化程序: $(“#TestButton”).click(function () { $.ajax({ url: ‘http://localhost:8082/Api/Test’, type: ‘GET’, dataType: ‘jsonp’, success: function(data) { alert(data.TestProperty); } }); }) 我已经检查了Fiddler,我得到的回答是: HTTP/1.1 504 Fiddler – Receive Failure Content-Type: text/html; charset=UTF-8 Connection: close Timestamp: 09:30:51.813 [Fiddler] ReadResponse() failed: The server did […]

在Asp.Net Web API中实现自定义模型绑定器时出错

我坚持这个非常奇怪的问题。 我有一个名为AttendanceController的API控制器派生自APIControllerFA ,它来自ApiController 这是代码 public class AttendanceController : ApiControllerFA { public HttpResponseMessage PostAttendance([ModelBinder(typeof(AttendanceRegistrationModelBinder))]AttendanceRegistrationModel model) { //checking model state for errors //throw new Exception(“Just to throw an error “); ……….. 从PostAttendance方法可以看出,我有一个名为AttendenceRegistrationModelBinder的自定义ModelBinder ,这是代码 public class AttendanceRegistrationModelBinder : DefaultModelBinder { protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.Name == “formJson”) { string val = […]

使用IFormFile拒绝ASP.Net对路径的核心访问

只需编写一个简单的ASP.NET Core WebAPI,并在使用接受IFormFiles的简单POST端点时: [HttpPost] public async Task<List> Post(List files) { long size = files.Sum(f => f.Length); List result = new List(); Console.WriteLine(files.Count); foreach (var f in files) { if (f.Length > 0) { Directory.CreateDirectory(“Resources”); using (var stream = new FileStream(“Resources”, FileMode.Create)) { await f.CopyToAsync(stream); result.Add(f.FileName); } } } return result; } 我收到此错误: System.UnauthorizedAccessException:拒绝访问路径’F:\ Documents HDD […]

什么是检查Web API是否可用的好/正确方法?

我有一个从API获取数据的应用程序: string jDoc = webclient.DownloadString(url); 然而,在我打电话之前,我需要确保API可用。 我该怎么做呢? 我应该只使用TRY/CATCH块还是有更好的方法吗?

MVC Web API绑定模型到派生类

我正在研究如何将模型绑定到MVC Web API中的派生类,我的问题是我认为我已经找到了5种方法… 我所拥有的是: 型号 – > 模型库 ModelA:ModelBase ModelB:ModelBase 控制器然后容器方法: Post(ModelBase model) {} 发布的数据将是ModelA或ModelB,我想向HTTP Request元数据添加信息(想想Content-Type:application / json; type = ModelA)并基于此告诉MVC将发布的内容绑定到A或B. 在代码中,我想象的是: Bind(request, bindingContext) { // check request headers and then… bindingContext.ModelType=typeof(ModelA); // let the framework continue doing its thing deserializing the content base.Bind(request, bindingContext); } 其他人怎么做到这一点? 或者你会如何推荐这样做? 我见过ParameterBinding,IModelBinder,MediaTypeFormatter等.MVC很棒,但有时很难想到你应该使用哪个钩子…… 编辑: 要添加更多信息,ModelBase很可能成为一个接口,并且将有数百个具体类。 它将用于CQRS:Command,然后是ConcreteCommandA,ConcreteCommandB,这些将被推送到调度程序,我不想为每个命令做一个动作,一个接收这些命令的中心动作,将它们反序列化为正确的类型并转发它们。

一小时后,身份从持有人令牌中消失

我正在使用Azure AD与Web应用程序和Web API建立多租户解决方案。 Web应用程序使用OpenIdConnect来检索承载令牌(在Azure Redis缓存中缓存),该令牌在Angular中用于从Web api获取JSON。 在Web应用程序和Web API(在Azure AD应用程序中设置)之间使用用户模拟。 问题: 这可以正常工作大约一个小时,然后身份突然消失在web api端。 如果我刷新Web应用程序,我会看到该页面被重定向到Microsoft登录页面,但不需要执行任何操作,因为用户只是重定向回Web应用程序并且一切都有效。 据我所知,Web应用程序在失败时使用相同的承载令牌,在再次运行时刷新(相同的过期时间)。 AuthenticationContext.AcquireTokenSilent适用于这两种情况。 我试图增加很多不同的超时,但没有任何帮助。 我还在web api上禁用了除持有者令牌身份validation之外的所有身份validation。 我不明白为什么身份会消失,为什么它有助于刷新客户。 有任何想法吗? 🙂 附加信息 这是RequestContext.Principal.Identity在登录或刷新后大约一小时查找的内容(在web api上): 这是大约一个小时后,导致身份validation失败: 我尝试过的一些代码更改: 在web api HttpConfiguration中: config.SuppressDefaultHostAuthentication(); config.Filters.Add( new HostAuthenticationFilter( new WindowsAzureActiveDirectoryBearerAuthenticationOptions().AuthenticationType)); 这将未经身份validation的主体从WindowsPrincipal更改为ClaimsPrincipal,但在一小时后仍然失败。 WindowsAzureActiveDirectoryBearerAuthenticationOptions BackChannelTimeout set to 5 days. Still fails 在Web应用程序web.config中: sessionState timeout=”525600″ for RedisSessionStateProvider. Still fails 在web应用程序owin auth进程中,增加了时间跨度并增加了滑动到期时间。 仍然失败: app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); […]

将整数数组发布到asp.net web api

我有一个类似下面的类发布到asp.net web api public class PostData { public int ID { get; set; } public int[] SelectedChoiceIDs { get; set; } } 在我的ApiController中有一个名为send的方法, PostData对象作为参数获取。 public bool Send(PostData data) { } 问题是每当我跟踪方法Web API没有绑定到整数数组时,也就是SelectedChoiceIDs属性。 我如何强制将整数数组绑定到“ SelectedChoiceIDs ”属性? 我发送的数据就像 { “ID” : 3 , “SelectedChoiceIDs” : [ 3,4,5,6 ] }

为什么为未使用的变量赋值会对此异步代码的function产生影响?

我有这个代码: private async Task SavePlatypusComplianceFileOnServer(string year, string month) { string monthAsMM = ReportRunnerConstsAndUtils.GetMonthAsMM(month); HttpClient client = new HttpClient(); client.BaseAddress = new Uri(ReportRunnerConstsAndUtils.SERVER_BASE_ADDRESS); String uriToCall = String.Format(“/api/PlatypusCompliance/{0}/{1}{2}”, _unit, year, monthAsMM); HttpResponseMessage response = await client.PostAsync(uriToCall, null); } ……工作正常 不过,看着它,我心里想,“自我,为什么你在不使用它时为”响应“赋值?为什么不在没有它的情况下调用方法?毕竟,Visual Studio或Resharper灰色用“回应”表示你没有实际意义/冗余。“ 所以我将代码更改为: private async Task SavePlatypusComplianceFileOnServer(string year, string month) { . . . // all but the […]