asp.net-web-api2

REST API Http status code best practices

Deadly 提交于 2019-12-24 02:28:08
问题 We are writing a REST API that's going to be exposed publicly and used by lot of third party developers. I am looking at best practices for http status codes especially in error cases. Our application has lots of components internally to which the API is the interface. If there are any errors in the internal components, should I return a 500 with appropriate error message ? When going through SO, I found some blogs / SO threads which suggested different ways but none of them had a concrete

Is it wrong to use routes versus query string parameters? [closed]

自闭症网瘾萝莉.ら 提交于 2019-12-24 01:47:10
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I have a Web API controller with two actions. One action returns a list of all entities from the database. The second action takes a query string parameter and filters the entities. The search action is wired up to use the query string parameter. It works but we encountered an

OData route exception

不打扰是莪最后的温柔 提交于 2019-12-24 01:24:11
问题 I am new to this so i will start with the code and after that i will explain. The problem is this [HttpGet, ODataRoute("({key})")] public SingleResult<Employee> GetByKey([FromODataUri] string key) { var result = EmployeesHolder.Employees.Where(id => id.Name == key).AsQueryable(); return SingleResult<Employee>.Create<Employee>(result); } [HttpGet, ODataRoute("({key})")] public SingleResult<Employee> Get([FromODataUri] int key) { var result = EmployeesHolder.Employees.Where(id => id.Id == key)

Web API not receiving the json of serialized object

Deadly 提交于 2019-12-24 01:09:52
问题 client side code: public async Task<ActionResult> Login(UserLoginModel user) { UserModel data = new UserModel { Username = user.Username, Password = user.Password.GenerateHash() }; var serializedData = JsonConvert.SerializeObject(data); var url = "http://localhost:55042/api/Login"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; httpWebRequest.Accept = "application/json"; using (var streamWriter =

Swagger UI displaying the asp.net webapi parameter name with dot notation

≯℡__Kan透↙ 提交于 2019-12-24 00:59:07
问题 I have configured Swagger for my asp.net webapi which is similar to one shown below [HttpGet] [Route("search")] public async Task<HttpResponseMessage> Get([FromUri]SearchCriteria searchCriteria) When i see the swagger documentation for the webapi , the parameter is displaying as searchCriteria.sortField searchCriteria.sortDirection and so on... being the sortField, sortDirection are properties of SearchCriteria How to get the parameter names without the object.propertyname format? Can anyone

Why my asp.net identity -user will log out automatically

帅比萌擦擦* 提交于 2019-12-24 00:59:07
问题 I have a project that have asp.net MVC and asp.net WebApi. I don't know why User log out automatically, for example when I close browser and after 15 minutes I see that I need to login again and after I redirect user to bank website for payment when the bank website redirect user again to my web site it need to login again. I use asp.net identity authentication cookie, below is in my StartUp.cs file code: public class Startup { public string Issuer { get; set; } public void Configuration

Windows Azure Deployment - not load file or assembly 'Newtonsoft.Json'

妖精的绣舞 提交于 2019-12-23 23:51:06
问题 I created MVC 4 application with Web API. It works good in local environment. When I deployed it to Azure, it gives following error. Exception information: Exception type: HttpException Exception message: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) at System.Web

Extending WebApi response using OWIN Middleware

随声附和 提交于 2019-12-23 23:13:14
问题 I have a WebApi project based on OWIN, and I wanted to extend the result of WebApi call with some additional data, for example add localization data to response. Initial idea was to inject that logic in a pipeline and once we get a result of API call I just have wrap that json with wrapper that will contain translation and some additional properties like time of execution. So I wrote my own middleware put it after UseWebApi() but it's not executed because WebApi doesn't call "Next" in case it

ASP.Net WebApi STA Mode

心已入冬 提交于 2019-12-23 17:04:09
问题 This post scratches off WebApi from being able to run in STA mode. But this post is also 2 years old. I've been digging into the newer WebApi2 but can't seem to find a way to make MessageHandlers run in STA mode. So has anyone found a way of running WebApi or WebApi2 in STA mode? 回答1: So it seems that if you want to switch the entire request processing thread to STA mode, you're better off with MVC. However, if your handler does a one off task that doesn't involve switching the entire thread

AllowAnonymous vs OverrideAuthorizeAttribute

无人久伴 提交于 2019-12-23 16:30:28
问题 What is the difference in the use of AllowAnonymous and OverrideAuthorizeAttribute. Is it same? 回答1: http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api The two are different, but can have the same effect in certain circumstances. Authentication is the process of verifying the user. Overrides disable the filter of the override type at the next highest level of scope. Authorization is the process of determining if the user should have access to a