asp.net-web-api

How do I get the real error message in HttpClient response.ReasonPhrase?

孤者浪人 提交于 2020-01-03 19:03:05
问题 My Setting I have 2 WebApi projects with the following flow: User makes request to API 1 API 1 makes request to API 2 on behalf of the user (using an HttpClient ). using (var client = new HttpClient()) { client.SetBearerToken(token); string endpoint = PbbSettings.Identity.Users.Delete.Replace("{userId}", userId); // Attempt deletion of the user using (var response = await client.DeleteAsync(endpoint)) { // Throw exception if not succeeded EnsureSuccess(response); } } The Problem So the flow

Web API and EF cause InvalidOperationException

自闭症网瘾萝莉.ら 提交于 2020-01-03 18:04:19
问题 I have 3 projects in my solution: DataModel (EF), DAL which works with Entities from DataModel and MVC Web API. There are only 2 very simple entities: Person, Address with 3 simple fields in each of them and Person has the Address field (so these 2 entities are linked) In my DAL I have a method that returns me the list of all the Persons, the content is very simple: return context.Person.ToList(); Now In my Web API I'm simply calling for the GetPersons() method and trying to return it. And

Use another auth method for external api calls

若如初见. 提交于 2020-01-03 17:50:08
问题 I have a Web API application with MVC. When a user is using the website, the authentication and authorization is currently automatically handled by the global forms authentication I use, configured in the Web.config like so: <authentication mode="Forms"> <forms loginUrl="~/Login" slidingExpiration="true" timeout="1800" defaultUrl="/"></forms> </authentication> <authorization> <deny users="?" /> </authorization> This makes sure only logged in users can access the site and call the API. But I

Posting Nested Collection to Web API

隐身守侯 提交于 2020-01-03 17:38:46
问题 I'm trying to post a complex type object to web api. On the web api side, when method recieves the object parameter, every property is set properly except a collection that is derived from ICollection. Here is my sample classes: public class MyClass { private int id; public int Id { get { return id; } set { id = value; } } private MyCollection<string> collection; public MyCollection<string> Collection { get { return collection; } set { collection = value; } } } public class MyCollection<T> :

Web API method return JSON data

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 17:20:24
问题 I am using ASP.net web API 2.0 and would like my method to return the data in JSON format only. Please suggest the code changes for this below method from the API controller class. public async Task<List<Partner>> GetPartnerList() { return await _context.Partners.Take(100).ToListAsync(); } 回答1: You can use the Json<T>(T content) method of the ApiController public async Task<IHttpActionResult> GetPartnerList() { List<Partner> data = await _context.Partners.Take(100).ToListAsync(); return Json

How to return Dictionary<complexType,int> with WebAPI

痞子三分冷 提交于 2020-01-03 17:19:06
问题 I'm providing a WebApi 2 endpoint that is done in this way: My controller is simply: public IDictionary<MyClass, int> GetMyClasses(string id) { Dictionary<MyClasses, int> sample = new Dictionary<MyClasses, int>(); sample.Add(new MyClasses() { Property1 = "aaa", Property2 = 5, Property3 = 8 },10); return sample; } The structure of MyClass is: public class MyClass { string Property1 {get;set;} int Property2 {get;set;} int Property3 {get;set;} } When I run my webservice, the helper webpage shows

NSubstitute: Mocking the request, response object inside a MVC/Web Api Controller?

走远了吗. 提交于 2020-01-03 17:10:54
问题 I am trying to find how to mock both the Request and Response objects that are available inside a controller from MVC / Web Api. Is this possible, I am not injecting the Request and Response objects, these are available because the controllers inherit from ApiController or Controller. Does anyone have some good examples for getting access to these through nsubstitute ? Also what about the other objects like context ? 回答1: You do not have to mock them. Since they have read/write properties,

Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'

只愿长相守 提交于 2020-01-03 16:59:45
问题 I have created One Filter Attribute public class AuthFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute { public AuthFilterAttribute() { } public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { } } I have registered this filter inside the global.asax.cs . When I am trying to call my web API using Postman it is showing the error: Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get

Is Owin/Katana supposed to replace Web API? [closed]

℡╲_俬逩灬. 提交于 2020-01-03 16:48:46
问题 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 3 years ago . When ASP.NET MVC came, Microsoft announced many times in many places that it wasn't supposed to replace ASP.NET Web Forms. In other words, it's just another technology that you might find useful, or you might use Web Forms in other scenarios. However, as companies enter into

Where is IHttpControllerFactory?

◇◆丶佛笑我妖孽 提交于 2020-01-03 13:41:22
问题 Microsoft website says there is a IHttpControllerFactory which can be used to generate controllers in custom ways. However, the System.Web.Http DLL referenced from Visual Studio: packages\Microsoft.AspNet.WebApi.Core.4.0.20710.0\lib\net40\System.Web.Http.dll Doesn't have that interface: So where is the interface now? 回答1: It's gone and replaced by a dependency resolver or more specifically the IDependencyResolver interface. Once you write your own dependency resolve you could plug it into the