asp.net-web-api2

Web API ModelBinding From URI

谁说胖子不能爱 提交于 2020-01-01 08:42:38
问题 So I have a custom Model Binder implemented for DateTime type and I register it like below: void Application_Start(object sender, EventArgs e) { // Code that runs on application startup GlobalConfiguration.Configuration.BindParameter(typeof(DateTime), new CurrentCultureDateTimeAPI()); } and then I have setup 2 sample actions to see if my custom model binding takes place: [HttpGet] public void BindDateTime([FromUri]DateTime datetime) { //http://localhost:26171/web/api/BindDateTime?datetime=09

OData Web API Query Interceptor

ぃ、小莉子 提交于 2019-12-31 06:59:07
问题 OData endpoint is created using ASP.NET Web API 2.0 Trying to create a Query Interceptor in the ODataController like shown in the below code: public class AVMItemController : ODataController { ADWAppContext sampleADW = new ADWAppContext("Server=XXX;Database=XXX;User ID=XXX;password=xxx;Trusted_Connection=false;Encrypt=true"); // GET: odata/AVM [EnableQuery(PageSize=25)] public IQueryable<ADWAppContext.AVMItem> GetAVMItems() { return sampleADW.AVMItems.AsQueryable<ADWAppContext.AVMItem>(); }

Different Models for RESTful GET and POST

旧街凉风 提交于 2019-12-31 02:26:06
问题 Does it violate the ideas of REST, or accepted conventions, to have different models for GET/PUT/POST at the same URL? An example: Consider a simple resource found at api/things I can create a thing by: POST api/things with body = { Name: "New Thing" } This returns me a thing along with location { Id: 500, Name: "New Thing", Links: ["api/things/500"] } Location Header: api/things/500 I can get the thing with: GET api/things/500 and I will get { Id: 500, Name: "New Thing", Links: ["api/things

How to use HttpClient to send a Request from a specific IP address? C#

霸气de小男生 提交于 2019-12-30 12:08:26
问题 I have multiple IPs on the server and would like to chose which one of those I want to use when using HttpClient class to get/post data from an API. (or to even send requests at the same time but utilise the 2 IPs and not just one) I have seen some examples using HttpWebRequest (here) that utilises delegate but I would like to carry on using HttpClient implementation. 回答1: [ This will be a hacky code, because there is no method/property to access the ServicePoint ] You can use reflection to

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

纵饮孤独 提交于 2019-12-30 08:13:16
问题 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

ASP.NET API versioning

只谈情不闲聊 提交于 2019-12-30 03:26:12
问题 I'm new to ASP.NET but I'm looking to implement some versioning for a new API I'm about to start. I'm not even sure if what I'm looking for is possible but I'm after a very clean version method using a header variable. Ideally I want to be able to have a versions folder within the code structure and different folders containing the different API versions within that. Each version folder would contain a full copy of the core API code so I'd know there would never be any conflicts etc. I know

ASP.NET Identity “Role-based” Claims

落爺英雄遲暮 提交于 2019-12-30 01:47:08
问题 I understand that I can use claims to make statements about a user: var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, "Peter")); claims.Add(new Claim(ClaimTypes.Email, "peter@domain.com")); But how should I store "role-based" claims? For example: The user is a super administrator. claims.Add(new Claim("IsSuperAdmin, "true")); The value parameter "true" feels completely redundant. How else can this statement be expressed using claims? 回答1: This is already done for you by

Resolving dependencies in OWIN WEB API Startup.cs with ninject

…衆ロ難τιáo~ 提交于 2019-12-29 18:43:52
问题 I have a Web Api 2 App, with two classes that both depend on another class, and i'm using ninject to resolve the dependancies. public class AuthorizationServerProvider : OAuthAuthorizationServerProvider { private IUserService _userService; public AuthorizationServerProvider(IUserService userService) { _userService = userService; } } public class RefreshTokenProvider : IAuthenticationTokenProvider { private IUserService _userService; public RefreshTokenProvider(IUserService userService) {

Override AccessTokenExpireTimeSpan

大憨熊 提交于 2019-12-29 07:36:32
问题 Is possible to override the default AccessTokenExpireTimeSpan for a specific ticket on a custom OAuthAuthorizationServerProvider? The default expiration time for all other tickets is 15 minutes. public public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { ... var ticket = new AuthenticationTicket(identity, properties); if (condition) { ticket.Properties.IssuedUtc = DateTime.UtcNow; ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddDays(14); }

How to pass Owin context to a Repo being injected into Api controller

你离开我真会死。 提交于 2019-12-28 12:05:34
问题 I've got a MVC WebApi owin (soft hosted) project, that uses Unity for resolving controller dependencies which look like this public class PacientaiController : ODataController { private readonly IEntityRepo<Ent.Pacientas> repo; public PacientaiController(IEntityRepo<Ent.Pacientas> repo) { this.repo = repo; } the problem I'm trying to solve - is how do I pass 'OwinContex' into a Repo. public class PacientasEntityRepo:IEntityRepo<Pacientas>,IDisposable { public PacientasEntityRepo(IOwinContext