Tag: asp.net web api2

webapi,如何使用自定义模型绑定器从POST / PUT操作读取文件

我有以下反应组件,显然它的工作正常: import React, { Component } from ‘react’; import { Row, Col } from ‘antd’; import PageHeader from ‘../../components/utility/pageHeader’; import Box from ‘../../components/utility/box’; import LayoutWrapper from ‘../../components/utility/layoutWrapper.js’; import ContentHolder from ‘../../components/utility/contentHolder’; import basicStyle from ‘../../settings/basicStyle’; import IntlMessages from ‘../../components/utility/intlMessages’; import { adalApiFetch } from ‘../../adalConfig’; export default class extends Component { constructor(props) { super(props); this.state […]

获取连接到C#.NET WebAPI应用程序的客户端的IP地址

我试过了: private const string HttpContext = “MS_HttpContext”; private const string RemoteEndpointMessage = “System.ServiceModel.Channels.RemoteEndpointMessageProperty”; public static string GetClientIpAddress(HttpRequestMessage request) { if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage]; if (remoteEndpoint != null) { return remoteEndpoint.Address; } } return null; } 根据: […]

我可以直接从HttpResponseMessage流式传输到文件而无需通过内存吗?

我的程序使用HttpClient向Web API发送GET请求,并返回一个文件。 我现在使用此代码(简化)将文件存储到光盘: public async Task DownloadFile() { var client = new HttpClient(); var uri = new Uri(“http://somedomain.com/path”); var response = await client.GetAsync(uri); if (response.IsSuccessStatusCode) { var fileName = response.Content.Headers.ContentDisposition.FileName; using (var fs = new FileStream(“C:\test\” + fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { await response.Content.CopyToAsync(fs); return true; } } return false; } 现在,当此代码运行时,该进程会将所有文件加载到内存中。 我实际上宁愿期望流从HttpResponseMessage.Content流式传输到FileStream,因此只有一小部分流被保存在内存中。 我们计划在大文件(> 1GB)上使用它,那么有没有办法在没有内存中的所有文件的情况下实现它? […]

在返回json’d数据时卸载“急切加载”属性导致问题

希望标题有意义,我会尽力描述我的问题。 我目前正在开发ASP.NET Web API。 在我的“公司”控制器中,我有一个GetCompany()动作。 /// /// Gets the ‘Authorization-Token’ request header and finds the company with the /// matching decrypted token from the database, returns it; if null, returns 404 /// /// Matching company for token passed in request or 404 if null [HttpGet][ResponseType(typeof(Company))] [Route(“api/company/”)] public HttpResponseMessage GetCompany() { string token = Request.Headers.GetValues(“Authorization-Token”).First(); ICollection […]

配置WebApi控制器的请求超时

我在我的WebAPi控制器中使用异步方法: public async Task SampleMethod(int subscriptionNumber, DateTime departureDate) { // […] } 如何配置请求超时? 该操作可能需要几分钟,我必须确保请求不会超时。 在MVC中有一个名为[AsyncTimeout]的属性。 WebApi中是否存在等价物? 可以全局配置吗?

不存在的目录/文件Web API(不是控制器)的自定义错误页面

我知道我可以为不存在的控制器或错误的路由设置自定义错误页面但是如果用户试图下载某个目录中不存在的文件,我该如何显示自定义错误页面? 我根本无法工作。 它仍然显示默认错误页面。

在ASP.Net Web Api 2中使用PUT动词上传文件

我想使用HTTP PUT动词公开ASP.Net Web Api 2动作来上传文件。 这与我们的REST模型一致,因为API代表远程文件系统(类似于WebDAV,但实际上是简化的),因此客户端选择资源名称(因此PUT是理想的,POST不是一个合理的选择)。 Web Api文档描述了如何使用multipart / form-data表单上载文件 ,但没有描述如何使用PUT方法执行此操作。 您将使用什么来测试这样的API(HTML多部分表单不允许PUT动词)? 服务器实现是否类似于web api文档中描述的多部分实现(使用MultipartStreamProvider ),或者应该如下所示: [HttpPut] public async Task PutFile(string resourcePath) { Stream fileContent = await this.Request.Content.ReadAsStreamAsync(); bool isNew = await this._storageManager.UploadFile(resourcePath, fileContent); if (isNew) { return this.Request.CreateResponse(HttpStatusCode.Created); } else { return this.Request.CreateResponse(HttpStatusCode.OK); } }

包含属性但排除该属性的属性之一

假设我的一个控制器中有这样的方法: [Route(“api/Products”)] public IQueryable GetProducts() { return db.Products .Include(p => p.Category); } 使用这个我可以从数据库中获取产品并包含其Category属性。 在我的CategoryControllerI中有这个方法: [Route(“api/Categories”)] public IQueryable GetCategories() { return db.Categories .Include(c => c.Parent) .Include(c => c.Products) .Include(c => c.SubCategories); } 当我向CategoryController发送GET请求时,它按预期工作,我得到类别,其父级,产品和子类别。 但是当我向ProductController发送GET请求时,我不想将所有产品都包含在所请求产品的类别中,我只需要有关该类别的基本信息。 那么,我如何让GetProducts()返回数据库中的产品,包括每个产品的Category属性,但不包括该类别的Products列表属性,仍然保留其他属性,如id,title等? 谢谢。

获取Content-Disposition参数

如何使用WebClient获取从WebAPI控制器返回的Content-Disposition参数? WebApi控制器 [Route(“api/mycontroller/GetFile/{fileId}”)] public HttpResponseMessage GetFile(int fileId) { try { var file = GetSomeFile(fileId) HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new MemoryStream(file)); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue(“attachment”); response.Content.Headers.ContentDisposition.FileName = file.FileOriginalName; /********* Parameter *************/ response.Content.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue(“MyParameter”, “MyValue”)); return response; } catch(Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex); } } 客户 void DownloadFile() { WebClient wc = […]

使用$ expand请求控制返回的内容

因此,使用ODataController ,您可以控制返回的内容,如果有人执行/odata/Foos(42)/Bars ,因为您将在FoosController上调用, FoosController所示: public IQueryable GetBars([FromODataUri] int key) { } 但是如果你想控制什么时候有什么东西/odata/Foos?$expand=Bars ? 你怎么处理那件事呢? 它会触发此方法: public IQueryable GetFoos() { } 而且我认为它只会在您返回的IQueryable上执行.Include(“Bars”) ,所以…我如何获得更多控制权? 特别是,我如何以OData不破坏的方式进行操作(例如$ select,$ orderby,$ top等继续工作。)