asp.net-web-api2

Replace IExceptionHandler in Web Api 2.2 with OWIN middleware exception handler

浪子不回头ぞ 提交于 2019-12-04 11:09:30
问题 I have created an OWIN middleware to catch exceptions. The middleware does nothing really but wrap the next call with try catch like this try { await _next(environment) } catch(Exception exception){ // handle exception } The problem is that the middlware is not capturing the exception because the exception is been handled by the default implementation of IExceptionHandler which returns an xml with the stack trace. I understand that I can replace the default IExceptionHandler implementation

WEB API 2 : get profile data during oauth RegisterExternal (facebook)

白昼怎懂夜的黑 提交于 2019-12-04 11:00:28
In the out of the box ASP.NET WEB API oAuth implementation after a new user calls: GET api/Account/ExternalLogins?returnUrl=%2F&generateState=true user is redirected to external log in (in my case Facebook) resulting in a token that they use for registration (out of the box code bellow) // POST api/Account/RegisterExternal [OverrideAuthentication] [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)] [Route("RegisterExternal")] public async Task<IHttpActionResult> RegisterExternal([FromBody]RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState)

Web Api 2 API not recognizing Multiple Attributes for Routing (Versioning)

穿精又带淫゛_ 提交于 2019-12-04 10:33:11
问题 I'm trying to implement both Attribute Routing and the VersionedRoute from RoutingConstaints Sample but when I use both on a controller, the versioned attribute no longer works. What would I need to modify on the attribute to get it to play nice with Attribute Routing? For code example download the sample project (or just look at the few files from the above link) and then modify the routes as such: // When I use the RoutePrefix, VersionedRoute no longer works (Sending "Api-Version" through

404 Not Found or Bad Request?

岁酱吖の 提交于 2019-12-04 10:20:09
问题 Let's say that we have the following REST call: GET api/companies/5 (get company with id 5) If company '5' doesn't exist, we would typically return a 404 Not Found response. But now, let's take this call: GET api/companies/5/invoices/10 (get invoice 10 from company 5) Now, if company '5' doesn't exist, do we still return a 404 Not Found ? Or should a 404 only be returned if the outer most resource can not be found (invoice 10, in this case). Would Bad Request perhaps be a better option? 回答1:

Web API Controller convert MemoryStream into StreamContent

匆匆过客 提交于 2019-12-04 10:11:42
问题 I have a large collection of images stored on a secured server some of which need to be displayed on a world facing portal. The portal's server is inside a DMZ which allows requests in but prevents direct requests from moving through to secured domain. The images are cataloged using SOLR and can be downloaded via an internal (apache?) server from http://intenalname/folderA/folderAB/file.jpg Inside my PhotoController I can create an instance of the WebClient , give it the url and get a

ASP.NET WebAPI default landing page

荒凉一梦 提交于 2019-12-04 10:08:02
问题 I've created a RESTful web service using ASP.NET WebApi v2 and I'm using Swashbuckle to generate swagger UI for API documentation. All API calls are under http://localhost/api/ and the swagger UI page is at http://localhost/browser/index (the 'browser' part is configurable). Browsing to http://localhost/ however will land on a empty page, so my question is is it possible to route http://localhost/ to http://localhost/browser/index so the user will be able to see the API documentation just by

WebApi2 attribute routing inherited controllers

梦想的初衷 提交于 2019-12-04 10:07:52
I'm trying to create basic REST api with a base controller like so: Base class: public abstract class WebApiEntityController<TEntity> : ApiController where TEntity : EntityBase<TEntity, int> { private readonly IRepository<TEntity> _repository; protected WebApiEntityController(IRepository<TEntity> repository) { _repository = repository; } [Route("")] [WebApiUnitOfWork] public HttpResponseMessage Get() { return Request.CreateResponse(HttpStatusCode.OK, _repository.ToList()); } [..........] Derived class: [RoutePrefix("api/TimesheetTask")] public class TimesheetTaskController :

'The parameters dictionary contains a null entry' error, Web API

戏子无情 提交于 2019-12-04 09:28:15
On my Web API I have a Document Controller with two simple actions: [AllowAnonymous] public class DocumentController : ApiController { public String Get(int id) { return "test"; } public String Get(string name) { return "test2"; } } The following URL (executes the first function) works fine: http://localhost:1895/API/Document/5 But this URL (should execute the second function): http://localhost:1895/API/Document/test Throws this error: { "message": "The request is invalid.", "messageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'

Getting post array in Asp.net MVC

為{幸葍}努か 提交于 2019-12-04 06:38:08
问题 I am writing a web api2 program that handles sms-contacts. There is a JSON post to the server like this: { "Id":"4", "ClientId":"2", "NoOfRecipient":"3", "Numbers": { "num1":9898776568, "num2":9087674589 }, "Msg":"This is a test" } and in Server I have a class to handle this JSON as below: public class SmsData { public int Id { get; set; } public int ClientId { get; set; } public int NoOfRecipient { get; set; } public IEnumerable<Numbers> Numbers { get; set; } public string Msg { get; set; }

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

本小妞迷上赌 提交于 2019-12-04 06:27:04
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 empty and none of the id's are passing through to the method. can't seem to understand if the problem is