Tag: asp.net web api

使用HttpClient时如何在HttpContent中设置大字符串?

所以,我创建了一个HttpClient并使用HttpClient.PostAsync()发布数据。 我使用了HttpContent HttpContent content = new FormUrlEncodedContent(post_parameters) ; 其中post_parameters是键值对List<KeyValuePair> 问题是,当HttpContent有一个很大的值(一个图像转换为base64要传输)我得到一个URL太长的错误。 这是有道理的 – 因为url不能超过32,000个字符。 但是,如果不是这样,我如何将数据添加到HttpContent ? 请帮忙。

如何使用自定义属性扩展IdentityUser

我正在使用asp.net Identity 2.0登录我的网站,其中身份validation详细信息存储在SQL数据库中。 Asp.net Identity已经以标准方式实现,可以在许多在线教程中找到。 IdentityModels的ApplicationUser类已扩展为包含自定义属性: public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager manager, string authenticationType) { CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, authenticationType); return userIdentity; } //My extended property public string Code { get; set; } } 当我注册一个新用户时,我在RegisterBindingModel传递Code自定义属性,但我不确定如何将此自定义属性插入WebUsers表。 我做了如下,但实际上并没有将这个属性与用户名和密码一起插入表中。 var user = new ApplicationUser() { UserName = userName, Email = model.Email, […]

如何使.Net web-API能够接受g-zipedpost

我有一个相当重要的标准.net MVC 4 Web API应用程序。 public class LogsController : ApiController { public HttpResponseMessage PostLog(List logs) { if (logs != null && logs.Any()) { var goodLogs = new List(); var badLogs = new List(); foreach (var logDto in logs) { if (logDto.IsValid()) { goodLogs.Add(logDto.ToLog()); } else { badLogs.Add(logDto.ToLogBad()); } } if (goodLogs.Any()) { _logsRepo.Save(goodLogs); } if(badLogs.Any()) […]

Web Api Request.CreateResponse HttpResponseMessage no intellisense VS2012

出于某种原因,在VS2012中, Request.CreateResponse现在是“红色”,当我将鼠标hover在IDE的使用状态时 无法解析符号’CreateResponse’ 这是ApiController类: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Filters; using GOCApi.Attributes; using GOCApi.Models; using GOCApi.Models.Abstract; using AttributeRouting; using AttributeRouting.Web.Http; namespace GOCApi.Controllers { [RoutePrefix(“Courses”)] public class CoursesController : ApiController { private ICoursesRepository _coursesRepository { get; set; } public CoursesController(ICoursesRepository coursesRepository) { _coursesRepository = coursesRepository; } [GET(“{id}”)] public […]

从PostAsJsonAsync获取响应

我有这行代码 var response = new HttpClient().PostAsJsonAsync(posturi, model).Result; 被调用的WebAPI控制器返回一个bool以确保该对象已保存,但如何返回该bool响应?

当url包含编码的&符号时,MVC WEB API路由失败

当我打电话给我的webservice时,我会得到两个参数: 从客户端(&)检测到潜在危险的Request.Path值。 Routeconfig: config.Routes.MapHttpRoute( name: “PropertiesSearch”, routeTemplate: “api/property/Search/{category}/{query}”, defaults: new { controller = “Property”, action = “Search”, category = “common”, query = string.Empty } ); Controllermethod: [HttpGet] public SearchResult Search(string category, string query) { } 当我打电话给api时: / API /属性/搜索/家庭/ areaID表示%3D20339%26areaId%3D20015 从客户端(&)检测到潜在危险的Request.Path值。 这样做: / API /属性/搜索/家庭/?查询= areaID表示%3D20339%26areaId%3D20015 工作良好。 如何解决路由解码问题?

ASP.NET WebAPI + Soap

WebAPI是否支持SOAP? 我正在尝试在MVC4中编写SOAP服务器,虽然我可以在WCF中完成,但似乎WebAPI正在取代它,但我认为没有办法在此使用SOAP,只使用REST Style接口的JSON / XML。

根据请求从MVC web api返回xml或json

鉴于以下webapiconfig; config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional } ); 和这个控制器; public class ProductsController : ApiController { Product[] _products = new Product[] { new Product { Id = 1, Name = “Tomato Soup”, Category = “Groceries”, Price = 1 }, new Product { Id = 2, Name = “Yo-yo”, Category = […]

JSON序列化类inheritance列表的属性

我有一个模型如下: public class TestResultModel { public bool Successful { get; set; } public string ErrorMessage { get; set; } } public class TestResultListModel : List { public int TotalTestCases { get { return base.Count; } } public int TotalSuccessful { get { return base.FindAll(t => t.Successful).Count; } } } 我从ApiController返回这个TestResultListModel : var testResultListModel = new […]

异步设置Thread.CurrentPrincipal?

使用ASP.NET WebAPI,在身份validation期间,设置Thread.CurrentPrincipal ,以便控制器稍后可以使用ApiController.User属性。 如果该身份validation步骤变为异步(以咨询另一个系统),则会丢失CurrentPrincipal任何突变(当调用者await恢复同步上下文时)。 这是一个非常简化的示例(在实际代码中,身份validation发生在动作filter中): using System.Diagnostics; using System.Security.Principal; using System.Threading; using System.Threading.Tasks; public class ExampleAsyncController : System.Web.Http.ApiController { public async Task GetAsync() { await AuthenticateAsync(); // The await above saved/restored the current synchronization // context, thus undoing the assignment in AuthenticateAsync(). Debug.Assert(User is GenericPrincipal); } private static async Task AuthenticateAsync() { // Save the […]