Tag: asp.net web api2

使用表单发布validationOAuth承载令牌

我使用Web API 2创建了一个基于OData的Web后端。这非常有效,在控制器上使用AuthorizeAttribute。 我希望能够通过标准的html表单和提交上传文件。 我们将OAuth令牌(通常在OData请求的标头中传递)作为隐藏输入字段传递。 问题:如何在控制器中validation?

如果返回格式是xml,如何删除web api中的模式节点?

我有一个web api方法,它将format作为一个参数,提供返回xml和json。方法返回的数据类型是DataTable.In json格式一切看起来很好但是xml格式的数据表的模式和xml节点中的一些其他属性还返回。如何返回仅包含datatable数据的简单xml?另外,我在WebApiConfig中使用QueryStringMapping。 这是WebApiConfig代码 public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{action}/{id}”, defaults: new { id = RouteParameter.Optional } ); config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping(“format”, “json”, new MediaTypeHeaderValue(“application/json”))); config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping(“format”, “xml”, new MediaTypeHeaderValue(“application/xml”))); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; } 这是控制器方法的伪代码 [BasicAuthentication] [Route(“api/{tablename}”)] [HttpGet] public IHttpActionResult Get(string tablename, string orders = “”, int limit = 100) { […]

Web API可选参数

我有一个带有以下签名的控制器: [Route(“products/filter/{apc=apc}/{xpc=xpc}/{sku=sku}”)] public IHttpActionResult Get(string apc, string xpc, int? sku) { … } 我用以下URI调用此方法: 〜/ API /产品/过滤?APC = AA&XPC = BB 〜/ API /产品/过滤?SKU = 7199123 第一个URI没有问题。 第二个有一个奇怪的副作用。 即使apc和xpc的默认值在未提供时应为null,但参数实际上是它们的名称。 我可以通过添加额外的逻辑来克服这个问题: apc = (apc == “apc”) ? null : apc; xpc = (xpc == “xpc”) ? null : xpc; 这似乎是一个黑客攻击,如果传递的值等于参数名称,则会出现问题。 有没有办法定义路线没有这种副作用?

Web API log4net不会在每个请求上创建新的日志文件

我想在每次发送API请求时创建一个新文件。 因此,我对此解决方案进行了一些修改,如下面的方法 它创建一个类似LogFile-2018.07.10-10_25_11的文件。 我正在通过postman测试API。 因此,当我运行我的程序并发送多个相同或不同API的请求时,它会在同一个日志文件中添加该进程。 它不会替换以前的日志详细信息,但会在新行中显示新请求。 这样,如果发送了多个请求,那么日志将相互混淆,我找不到哪个请求的日志详细信息。 创建新日志文件的唯一方法是停止我的程序然后运行它。 但是在生产环境中,API将随时可用。 如何在发送每个新请求时创建新的日志文件? 任何帮助将受到高度赞赏。

Web API Controller将MemoryStream转换为StreamContent

我有一大堆存储在安全服务器上的图像,其中一些需要在面向世界的门户上显示。 门户网站的服务器位于DMZ内部,允许请求但阻止直接请求进入安全域。 图像使用SOLR编目,可以通过内部(apache?)服务器从http://sofzh.miximages.com/c%23/file.jpg 在我的PhotoController我可以创建一个WebClient实例,给它提供url并获取一个MemoryStream 。 如果我尝试使用这个内存流来填充response.content,我会得到一个空响应(每个fiddler)。 如果我使用内存流写入本地文件,然后读取文件(使用FileStream和FileInfo)它“按预期”工作。 我应该能够从MemoryStream到StreamContent而无需通过文件系统(不应该)? 但怎么样? StreamContent(stream)的默认构造函数接受内存流实例而没有编译器错误……但它只是“不起作用”。 HttpResponseMessage response = Request.CreateResponse(); using (WebClient webClient = new WebClient()) { string url = string.Format(PHOTO_GET, filePath); using (MemoryStream memoryStream = new MemoryStream(webClient.DownloadData(url))) { // If these lines are unremarked the stream moves ‘through’ the file system and works (?!) //memoryStream.Position = 0; //string tempName = […]

Web Api 2 API无法识别路由的多个属性(版本控制)

我正在尝试从RoutingConstaints Sample实现属性路由和VersionedRoute ,但是当我在控制器上使用两者时,版本化属性不再起作用。 我需要修改属性以使其与属性路由一起使用? 对于代码示例,请下载示例项目(或者只查看上面链接中的几个文件),然后修改路由: // When I use the RoutePrefix, VersionedRoute no longer works (Sending “Api-Version” through http header doesn’t route correctly // If I remove the RoutePrefix I can use VersionedRoute again // What do I need to change in its code to be able to use both? [VersionedRoute(“api/Customers”, 1)] // This route […]

使用ASP Identity 2 POST到/ Token端点时始终收到’invalid_client’错误

大约一个月前,我有一个项目与ASP Identity OAuth完美配合。 我将使用grant_type,username和password向/ Token端点发送POST请求,所有这些都是花花公子。 我最近开始了一个基于Visual Studio 2013 RC2的SPA模板的新项目。 它与旧模板有点不同。 身份validation设置为非常基本的默认值, OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString(“/Token”), //AuthorizeEndpointPath = new PathString(“/Account/Authorize”), Provider = new ApplicationOAuthProvider(PublicClientId), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true }; 默认模板没有任何重大变化。 我可以通过我实现的Web API控制器方法成功注册帐户; // POST: /Account/Register [HttpPost] [AllowAnonymous] public async Task Register(RegisterBindingModel model) { if (ModelState.IsValid) { var user = […]

Web API自托管主机 – 在所有网络接口上绑定

如何在所有网络接口上进行Web API自主绑定? 我目前有以下代码。 不幸的是,它仅绑定在localhost上。 因此,从localhost以外的地方访问此服务器失败。 var baseAddress = string.Format(“http://localhost:9000/”); using (WebApp.Start (baseAddress)) { Console.WriteLine(“Server started”); Thread.Sleep(1000000); }

Web API Gzip未被应用

我添加了web.config条目以启用基于此S / O答案启用IIS7 gzip的 gzip压缩。 然后我在加载ASPX页面时检查了Chrome Developer窗口,并在响应中看到了标题: Cache-Control:private Content-Encoding:gzip Content-Length:3669 Content-Type:text/html; charset=utf-8 Date:Wed, 04 Mar 2015 00:46:05 GMT Server:Microsoft-IIS/7.5 Vary:Accept-Encoding X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET 这意味着它“正常”,对吗? 但是在进行Web API调用时查找该标头时,它不存在: Cache-Control:no-cache Content-Length:551 Content-Type:application/json; charset=utf-8 Date:Wed, 04 Mar 2015 00:53:05 GMT Expires:-1 Pragma:no-cache Server:Microsoft-IIS/7.5 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET 我尝试了各种不同的配置(从上面链接的S / O答案中推荐的配置开始)。 最后,在一个绝望的行为中,我将它设置为这个,我认为它会尝试压缩所有请求(除了* / *注释掉的所有内容): <!– –> <!– –> <!– –> 什么可以阻止GZIP应用于我的Web API方法? 更新 […]

如何将Autofac与WepApi 2和Owin集成?

我正在使用此软件包将Autofac与我的WebApi Owin应用程序集成: https://www.nuget.org/packages/Autofac.WebApi2.Owin 并关注这篇文章: http://alexmg.com/owin-support-for-the-web-api-2-and-mvc-5-integrations-in-autofac/ 我在Startup.cs中的代码如下所示: var config = new HttpConfiguration(); IContainer container = EngineContext.InitializeEngine(); var dependencyResolver = new AutofacWebApiDependencyResolver(container); config.DependencyResolver = dependencyResolver; app.UseAutofacMiddleware(container); app.UseAutofacWebApi(config); WebApiConfig.Register(config); app.UseWebApi(config); 无论我如何旋转它,重新排列代码或其他任何东西,Autofac都无法解决任何问题。 在Owin之前,我的Global.asax方法运行得很好: protected void Application_Start() { IContainer container = EngineContext.InitializeEngine(); var dependencyResolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver; GlobalConfiguration.Configure(WebApiConfig.Register); } 我错过了什么? 谢谢