asp.net-web-api2

ASP.NET OAuth Authorization - Difference between using ClientId and Secret and Username and Password

≡放荡痞女 提交于 2019-12-18 16:45:14
问题 I'm trying to implement a simple OAuthAuthorizationServerProvider in ASP.NET WebAPI 2. My main purpose is to learn how to have a token for a mobile app. I would like users to login with username & password, and then receive a token (and a refresh token so they won't have to re-enter credentials once token expires). Later on, I would like to have the chance to open the API for external use by other applications (like one uses Facebook api and such...). Here is how I've set-up my

Controlling what is returned with an $expand request

孤街醉人 提交于 2019-12-18 15:46:13
问题 So, using the ODataController , you get to control what gets returned if somebody does /odata/Foos(42)/Bars , because you'll be called on the FoosController like so: public IQueryable<Bar> GetBars([FromODataUri] int key) { } But what if you want to control what gets returned when somebody does /odata/Foos?$expand=Bars ? How do you deal with that? It triggers this method: public IQueryable<Foo> GetFoos() { } And I assume it just does an .Include("Bars") on the IQueryable<Foo> that you return,

How to get object using Httpclient with response Ok in Web Api

拟墨画扇 提交于 2019-12-18 14:49:17
问题 my web api like public async Task<IHttpActionResult> RegisterUser(User user) { //User Implementation here return Ok(user); } I am using HTTPClient to request web api as mentioned below. var client = new HttpClient(); string json = JsonConvert.SerializeObject(model); var result = await client.PostAsync( "api/users", new StringContent(json, Encoding.UTF8, "application/json")); Where i can find user object in my result request which is implemented on client application? 回答1: string Baseurl =

Enabling CORS through Web.config vs WebApiConfig and Controller attributes

混江龙づ霸主 提交于 2019-12-18 12:53:35
问题 There seems to be two functionally different ways to enable cross-origin request sharing in Web API 2. One is to import System.Web.Http.Cors , decorate a controller with the EnableCors attribute and to write config.EnableCors() in the WebApiConfig: [EnableCors(origins: "http://111.111.111.111", headers: "*", methods: "*")] public class GenericController : ApiController { // etc. The other is to modify the Web.config : <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control

Uniform, consistent error responses from ASP.Net Web API 2

可紊 提交于 2019-12-18 12:21:28
问题 I'm developing a Web API 2 application and I'm currently trying to format error resposnes in a uniform way (so that the consumer will also know what data object/structure they can inspect to get more info about the errors). This is what I've got so far: { "Errors": [ { "ErrorType":5003, "Message":"Error summary here", "DeveloperAction":"Some more detail for API consumers (in some cases)", "HelpUrl":"link to the docs etc." } ] } This works fine for exceptions thrown by the application itself

unable to configure Web API for content type multipart

匆匆过客 提交于 2019-12-18 12:16:29
问题 I am working on Web API's - Web API 2. My basic need is to create an API to update profile of the user. In this the ios and android will send me the request in multipart/form-data. They will send me few parameters with an image. But whenever i try to create the API, my model comes to be null every time. I have added this line in WebApiConfig also: config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data")); This is my class: public class

Timeout a Web Api request?

橙三吉。 提交于 2019-12-18 12:15:50
问题 Like MVC WebApi runs on the asynchronous ASP.NET pipeline, meaning execution timeout is unsupported. In MVC I use the [AsyncTimeout] filter, WebApi doesn't have this. So how do I timeout a request in WebApi? 回答1: Building on the suggestion by Mendhak, it is possible to do what you want, though not exactly the way you'd like to do it without jumping through quite a few hoops. Doing it without a filter might look something like this: public class ValuesController : ApiController { public async

MVC5, WebAPI2 and Ninject Parameterless constructor error

*爱你&永不变心* 提交于 2019-12-18 08:55:51
问题 So I have the exact opposite problem as MVC5, Web API 2 and Ninject I have a new MVC5/WebAPI2 project, that has both "Controller"s and "ApiControllers". I'm using the latest unstable version of Ninject.Web.WebAPI with no code changes to NinjectDependencyResolve.cs and Ninject.WebCommom.cs (besides binding my dependency) the ApiController's constructor injection works. However, when I call a MVC Controller I get: No parameterless constructor defined for this object. 回答1: The issue is you need

Query parameter route constraints

纵然是瞬间 提交于 2019-12-18 05:11:12
问题 I just started using ASP.NET Web API 2.1 and have encountered a limitation. Using Attribute Routing, I can do the following: [Route("item/{id:int}")] public IHttpActionResult GetItem(int id) { ... } The URL /item/5 will be routed to this action, but the URL /item/abc will not, because of the int constraint in {id:int} . I tried changing my URL so that the id parameter was in the query string along with its constraint, despite the use of route constraints on query parameters never being

Autofac dependency injection in implementation of OAuthAuthorizationServerProvider

≯℡__Kan透↙ 提交于 2019-12-18 03:10:14
问题 I am creating a Web Api application and I want to use bearer tokens for the user authentication. I implemented the token logic, following this post and everything seems to work fine. NOTE: I am not using the ASP.NET Identity Provider. Instead I have created a custom User entity and services for it. public class Startup { public void Configuration(IAppBuilder app) { ConfigureOAuth(app); var config = new HttpConfiguration(); var container = DependancyConfig.Register(); var dependencyResolver =