asp.net-web-api2

web api routing and http post [duplicate]

北慕城南 提交于 2019-12-01 04:07:39
This question already has an answer here: WebAPI - Attribute Routing POST not working with WebAPI Cors? 1 answer I'm building an API using WEB API 2. I have the following API controller: [RoutePrefix("api/account")] public class AccountController : ApiController { [Route("login")] [HttpPost] public IHttpActionResult AuthenticateUser(string username, string password) { if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return BadRequest("You must submit username and password"); } if (!Membership.ValidateUser(username, password)) { return BadRequest("Incorrect username or

Using Directory.Delete() and Directory.CreateDirectory() to overwrite a folder

我的梦境 提交于 2019-12-01 03:54:28
In my WebApi action method, I want to create/over-write a folder using this code: string myDir = "..."; if(Directory.Exists(myDir)) { Directory.Delete(myDir, true); } Directory.CreateDirectory(myDir); // 1 - Check the dir Debug.WriteLine("Double check if the Dir is created: " + Directory.Exists(myDir)); // Some other stuff here... // 2 - Check the dir again Debug.WriteLine("Check again if the Dir still exists: " + Directory.Exists(myDir)); Issue Strangely, sometimes right after creating the directory, the directory does not exist! Sometimes when checking the dir for the first time (where the

web api get route template from inside handler

谁都会走 提交于 2019-12-01 03:54:12
I searched a lot before putting the questions here but the more I search the more confused I get. So I have created an handler and I am trying to get the route like this: public class ExecutionDelegatingHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (securityAuthority.VerifyPermissionToExecute(request.GetRouteData().Route.RouteTemplate, request.Headers)) { return base.SendAsync(request, cancellationToken); } else { httpResponseMessage.StatusCode = HttpStatusCode.Unauthorized; } } }

Automatically bind pascal case c# model from snake case JSON in WebApi

北慕城南 提交于 2019-12-01 03:53:06
问题 I am trying to bind my PascalCased c# model from snake_cased JSON in WebApi v2 (full framework, not dot net core). Here's my api: public class MyApi : ApiController { [HttpPost] public IHttpActionResult DoSomething([FromBody]InputObjectDTO inputObject) { database.InsertData(inputObject.FullName, inputObject.TotalPrice) return Ok(); } } And here's my input object: public class InputObjectDTO { public string FullName { get; set; } public int TotalPrice { get; set; } ... } The problem that I

asp.net web api 2 CORS and authentication authorization configuration

帅比萌擦擦* 提交于 2019-12-01 03:51:18
问题 I've created an asp.net web api 2 service with individual account security. I'm trying to call it form AngularJs as per this example: http://www.codeproject.com/Articles/742532/Using-Web-API-Individual-User-Account-plus-CORS-En could not get that to work so added some config from here: How to make CORS Authentication in WebAPI 2? and can't get past this error: XMLHttpRequest cannot load 'serverRegisterUrl'. The 'Access-Control-Allow-Origin' header contains multiple values 'clientUrl, *, *',

Exception handling in Web API

橙三吉。 提交于 2019-12-01 03:43:02
In my Web API project, I created sub projects (class libraries) where I handle actual data handling operations. My backend database is DocumentDB. My question is how do I tell my Web API action methods of any errors I may encounter within data methods in my class libraries? Once my Web API method knows about the error, I can just return Http status 500 or something like that but I'm not sure what I should have in the catch part (see below) and how I can notify the calling Web API method of the error encountered? --- Web API Method --- public async Task<IHttpActionResult> DoSomething(Employee

How to prevent ODataConventionModelBuilder to automatically expose all derived types' metadata?

↘锁芯ラ 提交于 2019-12-01 03:39:56
I'm using ODataConventionModelBuilder to build Edm Model for Web API OData Service like this: ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.Namespace = "X"; builder.ContainerName = "Y"; builder.EntitySet<Z>("Z"); IEdmModel edmModel = builder.GetEdmModel(); Class Z is located in one assembly, and there is public class Q derived from Z located in different assembly. The ODataConventionModelBuilder will generates Edm Model that includes definition of class Q (among other derived classes) and it will be exposed with service metadata. That is undesirable in our case. When

Configure request timeout for WebApi controllers

人走茶凉 提交于 2019-12-01 03:21:23
I'm using async methods in my WebAPi controllers: public async Task<HttpResponseMessage> SampleMethod(int subscriptionNumber, DateTime departureDate) { // [...] } How do I configure the request timeout? The operation can take up to a couple of minutes and I have to make sure that the request do not timeout. In MVC there is an attribute called [AsyncTimeout] . Are there an equivalent in WebApi? Can it be configured globally? Good question, I would recommend to handle this from client side - you can always specify timeout settings in your consumer code, even if it is ajax: $.ajax({ url: "/ajax

Anatomy of an OWIN Startup

自闭症网瘾萝莉.ら 提交于 2019-12-01 03:19:06
问题 What are all the hooks on the OWIN Startup class? Information on these is scarce. For example, one required hook on every Startup class is that it should have a Configuration method. This information can be gathered from the Microsoft documentation. class Startup { public void Configuration(IAppBuilder appBuilder) { ... } } What is the rationale behind not having an IOwinStartup interface or OwinStartup base class in the framework? interface IOwinStartup { void Configuration(IAppBuilder

Setting Result in the context of ChallengeAsync method in an authentication filter

僤鯓⒐⒋嵵緔 提交于 2019-12-01 02:41:38
问题 This question is related to the answer I have provided here. OP's comment got me thinking a bit. I suggested using a class implementing IHttpActionResult like this in the ChallengeAsync method of the authentication filter. public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken) { context.Result = new ResultWithChallenge(context.Result); return Task.FromResult(0); } public class ResultWithChallenge : IHttpActionResult { private readonly