草庐IT

c# - 带有额外参数的 Webapi 表单数据上传(到 DB)

我需要上传发送额外参数的文件。我在stackoverflow中找到了以下帖子:Webapiajaxformdatauploadwithextraparameters它描述了如何使用MultipartFormDataStreamProvider并将数据保存到文件服务器来执行此操作。我不需要将文件保存到服务器,而是保存到数据库。我已经有使用MultipartMemoryStreamProvider的工作代码,但它不使用额外的参数。你能告诉我如何在webapi中处理额外的参数吗?例如,如果我添加文件并测试参数:data.append("myParameter","test");这是我的web

c# - WebAPI 和 ODataController 返回 406 Not Acceptable

在将OData添加到我的项目之前,我的路线设置如下:config.Routes.MapHttpRoute(name:"ApiById",routeTemplate:"api/{controller}/{id}",defaults:new{id=RouteParameter.Optional},constraints:new{id=@"^[0-9]+$"},handler:sessionHandler);config.Routes.MapHttpRoute(name:"ApiByAction",routeTemplate:"api/{controller}/{action}",defau

c# - 在 WebApi 或 MVC Controller 中使用 ConfigureAwait(false) 有什么危险吗?

假设我有两种情况:1)WebApiController[System.Web.Http.HttpPost][System.Web.Http.AllowAnonymous][Route("api/registerMobile")]publicasyncTaskRegisterMobile(RegisterModelmodel){varregisterResponse=awaitAuthUtilities.RegisterUserAsync(model,_userService,User);if(registerResponse.Success){varresponse=await_use

c# - WebApi - 从 Uri 和 Body 绑定(bind)

是否可以同时从Uri和Body绑定(bind)一个模型?例如,给定以下内容:routes.MapHttpRoute(name:"APIDefault",routeTemplate:"api/{controller}/{id}",defaults:new{id=RouteParameter.Optional});publicclassProductsController:ApiController{publicHttpResponseMessagePut(UpdateProductmodel){}}publicclassUpdateProduct{intId{get;set;}strin

c# - WebAPI 中 DependencyResolver.SetResolver 和 HttpConfiguration.DependencyResolver 有什么区别

我有现有的项目,它使用AutoFac作为IoC。在注册码中我有这些行:varresolver=builder.Build();DependencyResolver.SetResolver(newAutofacDependencyResolver(resolver));config.DependencyResolver=newAutofacWebApiDependencyResolver(resolver);所以我的问题是DependencyResolver.SetResolver和HttpConfiguration.DependecyResolver有什么区别?为什么我要同时分配它们?

c# - WebAPI 文件上传 - 无需将文件写入磁盘

所有关于处理使用FormData上传到ASP.NETWebAPI处理程序的文件的文档/教程/问题都使用MultipartFormDataStreamProvider来处理多部分流以将其拆分为相关的表单字段和文件。varroot=HttpContext.Current.Server.MapPath("~/App_Data");varprovider=newMultipartFormDataStreamProvider(root);awaitRequest.Content.ReadAsMultipartAsync(provider);foreach(MultipartFileDatafil

c# - 从 WebAPI 2 端点返回自定义 HTTP 状态代码

我正在WebAPI2中处理一项服务,端点当前返回一个IHttpActionResult。我想返回状态代码422,但由于它不在HttpStatusCode枚举中,我不知道如何发送它,因为所有的构造函数需要HttpStatusCode参数就目前而言,我正在返回BadResult(message),但返回422+消息对我的客户来说更具描述性和有用性。有什么想法吗? 最佳答案 根据C#规范:Thesetofvaluesthatanenumtypecantakeonisnotlimitedbyitsenummembers.Inparticul

c# - 如何使用来自 asp.net Web API 的 webApi 将结果存储在数据库中?

我想知道如何使用来自另一个ASP.NetWebAPI的WEBAPI来将响应存储在数据库中。我知道如何从javascript、控制台应用程序等客户端使用WEBAPI。但要求是通过我的WEBAPI从第三方API提取数据并将结果存储在数据库中,以便使用我的WEBAPI我的客户向我请求数据。是否可以使用Asp.NetWebAPI执行此操作? 最佳答案 本教程解释了如何使用C#使用webapi,在此示例中使用控制台应用程序,但您也可以使用另一个webapi来使用当然。http://www.asp.net/web-api/overview/we

c# - 当我有程序集时,WebAPI 发现引用错误

我已经在我的解决方案中创建了一个MVC4WebAPI应用程序,但我现在遇到了2个错误,我需要一些帮助。'System.Web.Http.HttpConfiguration'doesnotcontainadefinitionfor'MapHttpAttributeRoutes'andnoextensionmethod'MapHttpAttributeRoutes'acceptingafirstargumentoftype'System.Web.Http.HttpConfiguration'couldbefound(areyoumissingausingdirectiveoranassem

c# - ASP.NET WebAPI : how to perform a multipart post with file upload using WebApi HttpClient

我有一个WebApi服务处理来自简单表单的上传,如下所示:但是,我不知道如何使用HttpClientAPI模拟同一个帖子。FormUrlEncodedContent位很简单,但如何将文件内容与名称添加到帖子中? 最佳答案 经过反复试验,下面是实际有效的代码:using(varclient=newHttpClient()){using(varcontent=newMultipartFormDataContent()){varvalues=new[]{newKeyValuePair("Foo","Bar"),newKeyValuePai