Tag: asp.net web api

所有Entity Framework方法都应该使用异步吗?

在Asp.Net MVC或Asp.Net Web API中,使用查询数据库(即使是最简单的查询)的每个控制器操作都使用async / await模式是不错的做法? 我知道使用async / await会增加复杂性,但是添加它值得吗? 即使是最简单的查询?

从catch块向客户端发送格式化的错误消息

我使用以下代码从filter(ActionFilterAttribute)向客户端发送错误消息。 catch (Exception) { var response = context.Request.CreateResponse(httpStatusCode.Unauthorized); response.Content = new StringContent(“User with api key is not valid”); context.Response = response; } 但问题是它只以纯文本forms发出。 我想将其作为当前格式化程序的格式发送。 像json或xml的forms。 在这里我知道这是因为我正在使用StringContent()。 但是我们如何使用自定义Error对象编写? 比如,以下不起作用: response.Content = new Error({Message = “User with api key is not valid”}); 我们如何为此编写代码? 提前致谢。

处理同一设备的azure通知中心中的重复条目

我在我的应用程序中使用Windows azure通知集线器向用户提供通知。 以下是在通知中心上注册设备的API代码。 var platform = registrationCall[“platform”].ToString(); var installationId = registrationCall[“instId”].ToString(); var channelUri = registrationCall[“channelUri”] != null ? registrationCall[“channelUri”].ToString() : null; var deviceToken = registrationCall[“deviceToken”] != null ? registrationCall[“deviceToken”].ToString() : null; string RegistrationID = registrationCall[“RegistrationID”] != null ? registrationCall[“RegistrationID”].ToString() : null; var userName = HttpContext.Current.User.Identity.Name; RegistrationDescription registration = null; AppleRegistrationDescription iosExistingRegistrationByDeviceToken = null; string UserID […]

是否所有Web请求都是并行执行的并且是异步处理的?

我正在使用由IIS托管的WebApi服务控制器,我正在尝试理解这种架构是如何工作的: 当WebPage客户端同时发送异步请求时,所有这些请求是否都在WebApi控制器上并行执行? 在IIS应用程序池中,我注意到队列大小设置为1,000默认值 – 这是否意味着1,000个最大线程可以同时在WebApi服务器上并行工作? 或者这个值只与IIS队列有关? 我已经读过IIS维护某种线程队列,这个队列是异步发送它的工作吗? 或者IIS发送给WebApi服务的所有客户端请求是否同步发送?

如何正确调用ASP .NET MVC WebAPI 2方法

我为ASP.NET MVC WebAPI 2创建了分离项目,我想调用Register方法。 (我使用的项目默认是由VS2013创建的。) [Authorize] [RoutePrefix(“api/Account”)] public class AccountController : ApiController { …. // POST api/Account/Register [AllowAnonymous] [Route(“Register”)] public async Task Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } IdentityUser user = new IdentityUser { UserName = model.UserName }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null) […]

使用angularjs将多个对象发送到webapi

我的ASP.NET WebApi应用程序中有以下控制器: [Route(“api/PutItem/”)] [HttpPut] public IHttpActionResult PutItem(Guid id, Item item) { if (!ModelState.IsValid) } 我在“Items”AngularJs服务中有以下内容 var doEditItem = function(item){ var deferred = $q.defer(); console.log(‘item’, item); var config = { headers: { ‘Authorization’: “Bearer ” + $rootScope.token } //,params: {id:item.ItemId} } $http.put(sitesettings.url() + “api/PutItem/”, item, config) .success(function(data){ deferred.resolve(data); })…. 我最终收到这条消息: 没有找到与请求URI匹配的HTTP资源’ http:// localhost:63289 / api / […]

如何使用Ninject InRequestScope处理异步调用?

我们在ASP.NET Web Api应用程序中使用Ninject ,并将DbContext与InRequestScope绑定。 这适用于我们的大多数请求,因为它们同步完成所有工作,因此在请求完成后可以安全地处理上下文。 但是,我们根据请求进行异步Web服务调用,它具有作为回调传递的continuation方法,并且该回调方法需要使用数据库上下文。 但是我们的请求不应该等待异步服务调用完成,而是立即返回(这是一个明确的要求)。 这是一个简化的情况示例。 public class MyController : ApiController { private readonly MyDbContext dbContext; private readonly SomeWebService service; public MyController(MyDbContext dbContext, SomeWebService service) { this.dbContext = dbContext; this.service = service; } public IHttpActionResult MyActionWithAsyncCall() { // Doing stuff. // Calling webservice method, passing the Callback as the continuation. service.MethodWithCallback(param1, param2, this.Callback); […]

JSON.NET将DateTime.MinValue序列化为null

我想将设置为我的Web API返回的DateTime字段序列化为NULL而不是”0001-01-01T00:00:00″ 。 我知道有一种方法可以让JSON.NET省略设置为默认值的字段,但我更喜欢JSON.NET将DateTime MinValue / “0001-01-01T00:00:00” as null专门序列化为DateTime MinValue / “0001-01-01T00:00:00” as null 。 有没有办法做到这一点?

名为“DefaultApi”的路径已在路径集合中

这个问题可能看似重复,但这有点不同。 在SO的所有其他问题中,我注意到他们注册了多条路线。 但就我而言,我只有一条路线。 我正在创建asp.net webapi(框架4.5)并且在RegisterRoutes()方法中只有一个路由 – public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute( name: “DefaultApi”, url: “rest/{controller}/{id}”, defaults: new { id = UrlParameter.Optional } ); } 那为什么会抛出错误? A route named ‘DefaultApi’ is already in the route collection. Route names must be unique. Parameter name: name

在ASP.NET WebApi 2中自定义承载令牌JSON结果

我演练了这个官方的ASP.NET教程 ,持票令牌发布如下JSON。 { “access_token”:”boQtj0SCGz2GFGz[…]”, “token_type”:”bearer”, “expires_in”:1209599, “userName”:”Alice”, “.issued”:”Mon, 14 Oct 2013 06:53:32 GMT”, “.expires”:”Mon, 28 Oct 2013 06:53:32 GMT” } 我想添加用户配置文件属性以及上述结果,以减少来自客户端的请求数。 示例如下…… { “access_token”:”boQtj0SCGz2GFGz[…]”, “token_type”:”bearer”, “expires_in”:1209599, “userName”:”Alice”, “.issued”:”Mon, 14 Oct 2013 06:53:32 GMT”, “.expires”:”Mon, 28 Oct 2013 06:53:32 GMT”, “Notifications”:35, “IsEventMember”:true, “Promotion”:324372 } 我使用的oauth提供程序来自默认的ASP.NET模板( ApplicationOAuthProvider.cs ), OAuthOption如下所示。 OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new […]