asp.net-web-api2

How to send a list of integers to web api 2 get request?

别来无恙 提交于 2019-12-06 00:53:12
问题 I am trying to accomplish this task in which I need to send a list of id's (integers) to a web api 2 get request. So I've found some samples here and it even has a sample project, but it doesn't work... Here is my web api method code: [HttpGet] [Route("api/NewHotelData/{ids}")] public HttpResponseMessage Get([FromUri] List<int> ids) { // ids.Count is 0 // ids is empty... } and here is the URL which I test in fiddler: http://192.168.9.43/api/NewHotelData/?ids=1,2,3,4 But the list is always

DbContext is Disposed When Using Unity Dependency Injection on WebApi project

隐身守侯 提交于 2019-12-06 00:12:39
I'm fairly new at using dependency injection and I think I must be overlooking something really simple. I have a Web API project where I'm registering generic repositories. The repositories take a dbContext as a parameter in their constructor. The behavior I find strange is that I can make one successfull call to the service but any subsequent calls tell me that the dbcontext has been disposed. I do have a using statement in there but that shouldn't be a problem since DI is supposed to be creating new instances of my dependencies for each web request(although I could be wrong). Here is my

WebAPI CORS - why is the OPTIONS request making its way into my Controller?

天大地大妈咪最大 提交于 2019-12-05 23:26:36
问题 I have CORS working with the following: [System.Web.Http.HttpPut] [System.Web.Http.AcceptVerbs("OPTIONS")] [System.Web.Http.Route("api/exercise")] public HttpResponseMessage UpdateExercise(Exercise exercise) { try { _adminService.UpdateExercise(exercise); return Request.CreateResponse(HttpStatusCode.OK, "Success"); } catch (Exception e) { return Request.CreateResponse(HttpStatusCode.InternalServerError, e); } } In my global.asax : protected void Application_BeginRequest() { if (Request

Required attribute on action parameter doesn't work

◇◆丶佛笑我妖孽 提交于 2019-12-05 23:08:58
The code is like: [HttpPost] public ResultEntityVM Register([FromBody,Required] RegisterParam createAssessorParam) { if (ModelState.IsValid == false) { return null; } //other code ResultEntityVM vm = new ResultEntityVM(); return vm; } When the parameter createAssessorParam is null , the value of ModelState.IsValid is true . Why? If I want to auto judge the parameter is null or not, what can I do? Don't I can only write the code: if(RegisterParam != null) { //other } I've run into the same problem and solved it by implementing a custom action filter attribute evaluating all the validation

Newtonsoft.json IsoDateTimeConverter and DateFormatHandling

我的梦境 提交于 2019-12-05 22:50:35
This is not an issue but concept I just want to make it clear. When working with asp.net web api we use Newtonsoft.Json's SerializerSettings . In this setting we can set jsonSetting.Converters.Add(new IsoDateTimeConverter()); jsonSetting.DateFormatHandling = DateFormatHandling.IsoDateFormat; I understood what IsoDateTimeConverter is doing but but not clear about DateFormatHandling.IsoDateFormat . It seems to be there is no effect of DateFormatHandling.MicrosoftDateFormat if you still using IsoDateTimeConverter . Still excepting some explanation who know about this. dbc You actually have

Asp.net Web API 2 and mixed Authentication using both Integrated Windows and Token based

此生再无相见时 提交于 2019-12-05 22:16:07
I have an asp.net Web API server running under IIS, that until now has used windows authentication as it has only had other services running on the same domain conencting to it. So, in my web.config I have the following settings... <system.web> <compilation debug="true" targetFramework="4.5.1" /> <httpRuntime targetFramework="4.5.1" /> <authentication mode="Windows" /> </system.web> <system.webServer> <security> <authentication> <windowsAuthentication enabled="true" /> </authentication> </security> .... With this I can use a browser (or the services) on the same domain and reach my services.

Url Referrer is not available in WebApi 2 MVC project

烂漫一生 提交于 2019-12-05 22:05:38
I have an MVC WebAPI 2 project with a Controllers controller. The Method I'm trying to call is POST (Create). I need to access the referring URL that called that method yet, no matter what object I access, the referring URL either doesn't exist in the object or is null. For example, I've added the HTTPContext reference and the following returns null : var thingythingthing = HttpContext.Current.Request.UrlReferrer; The Request object does not have a UrlReferrer property. This returns null as well: HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] I cannot modify the headers because I

JWT token not accepted by Web APi2 authentication with JwtAuthForWebAPI

泪湿孤枕 提交于 2019-12-05 21:11:41
I followed the tutorial but it's seems not working properly with JWT, but it was ok for basic authentication. I downloaded and installed JwtAuthForWebAPI. I also generated JWT token and trying to do a API call but error is HTTP/1.1 401 Unauthorized . Do I have to implement/modify anything to transfer claims from JWT to Thread.CurrentPrincipal & HttpContext.CurrentPrincipal? My code is quite simple: global.asax.cs: GlobalConfiguration.Configuration.MessageHandlers.Add( new JwtAuthenticationMessageHandler { AllowedAudience = reader.AllowedAudience, Issuer = reader.Issuer, SigningToken = builder

Elmah.axd on WebAPI 2.2 - No HTTP Resource was found

↘锁芯ラ 提交于 2019-12-05 18:26:05
I'm trying to access /elmah.axd in my broswer, but it returns: {"message":"No HTTP resource was found that matches the request URI 'http://services.domain.com/elmah.axd'."} The server is local (127.0.0.1) and even on that I have my web.config Elmah settings to secure it this way: <elmah> <security allowRemoteAccess="true" /> </elmah> My WebApiConfig looks like: public static void Register(HttpConfiguration config) { // Web API configuration and services // Locally only you will be able to see the exception errors config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly; // Web API

Complex JSON Web Token Array in webapi with OWIN

拜拜、爱过 提交于 2019-12-05 17:47:18
I'm in process of trying to learn JWT and ouath. I have came across form of JWT which would help me in development of my authorization server. The format I came across is following : { iat: 1416929061, jti: "802057ff9b5b4eb7fbb8856b6eb2cc5b", scopes: { users: { actions: ['read', 'create'] }, users_app_metadata: { actions: ['read', 'create'] } } } However since in adding claims we can only add simple string how something like this can be achieved ? The only way I have seen till now was to use JSON.serialization - coming from https://stackoverflow.com/a/27279400/2476347 new Claim(someClass