asp.net-web-api2

webapi2 return simple string without quotation mark

非 Y 不嫁゛ 提交于 2019-12-01 01:51:50
问题 Simple scenario: public IHttpActionResult Get() { return Ok<string>("I am send by HTTP resonse"); } returns: "I am send by HTTP resonse" Just curious, can I avoid the quotation mark in other words return: I am send by HTTP resonse or is this necessary in HTTP? 回答1: Yes you can avoid the " public class ValuesController : ApiController { public string Get() { return "qwerty"; } } now check http://localhost:3848/api/values and response <string xmlns="http://schemas.microsoft.com/2003/10

Custom error pages for non-existant directory/file Web API (not controllers)

a 夏天 提交于 2019-12-01 01:26:02
I know I can have custom error pages for non existent controllers or wrong routing but how can I show custom error pages if an user tries to download a file that does not exist inside some directory? I can't make it work at all. It's still showing default error page. This blog post walks you through handling 404 errors in ASP.Net Web API: http://weblogs.asp.net/imranbaloch/handling-http-404-error-in-asp-net-web-api Let's say that you are developing a HTTP RESTful application using ASP.NET Web API framework. In this application you need to handle HTTP 404 errors in a centralized location. From

Can I directly stream from HttpResponseMessage to file without going through memory?

て烟熏妆下的殇ゞ 提交于 2019-12-01 00:47:52
问题 My program uses HttpClient to send a GET request to a Web API, and this returns a file. I now use this code (simplified) to store the file to disc: public async Task<bool> DownloadFile() { var client = new HttpClient(); var uri = new Uri("http://somedomain.com/path"); var response = await client.GetAsync(uri); if (response.IsSuccessStatusCode) { var fileName = response.Content.Headers.ContentDisposition.FileName; using (var fs = new FileStream("C:\test\" + fileName, FileMode.Create,

web api get route template from inside handler

为君一笑 提交于 2019-12-01 00:43:53
问题 I searched a lot before putting the questions here but the more I search the more confused I get. So I have created an handler and I am trying to get the route like this: public class ExecutionDelegatingHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (securityAuthority.VerifyPermissionToExecute(request.GetRouteData().Route.RouteTemplate, request.Headers)) { return base.SendAsync(request,

How to prevent ODataConventionModelBuilder to automatically expose all derived types' metadata?

南楼画角 提交于 2019-12-01 00:29:08
问题 I'm using ODataConventionModelBuilder to build Edm Model for Web API OData Service like this: ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.Namespace = "X"; builder.ContainerName = "Y"; builder.EntitySet<Z>("Z"); IEdmModel edmModel = builder.GetEdmModel(); Class Z is located in one assembly, and there is public class Q derived from Z located in different assembly. The ODataConventionModelBuilder will generates Edm Model that includes definition of class Q (among other

Loading Image in JavaScript with Bearer token

帅比萌擦擦* 提交于 2019-11-30 22:00:35
I am loading an image in JS like this: var img = new Image(); img.onload = function () { .. }; img.src = src; This will work, but I have realized that I must secure my images on the server side with OAuth 2 (as with the rest of the application) and this will effect in me simply receiving a 401 Unauthorized. This is an angular app and I do have an interceptor adding the Authorization header consequently for all the angular service requests to the server, but in this case of course - the interceptor is not used because the call is not made in an angular context. Any ideas to how I can add the

How to Instantiate ODataQueryOptions

偶尔善良 提交于 2019-11-30 21:17:08
I have a working (simplified) ODataController with the following method. public class MyTypeController : ODataController { [HttpGet] [EnableQuery] [ODataRoute("myTypes")] public IQueryable<MyType> GetMyTypes(ODataQueryOptions<MyType> options) { return _repo.myResultsAsQueryable(); } } I would like to be able to call this method from the server and to do this I need to instantiate an ODataQueryOptions which requires an ODataQueryContext . There are examples of how to do this (Eg. here and here ) but they all seem to reference a previous version of OData. The ODataQueryContext constructor

Web API with OWIN throws ObjectDisposedException for HttpMessageInvoker

不打扰是莪最后的温柔 提交于 2019-11-30 20:02:59
I am having some difficulties with OWIN in a ASP.NET WebApi2 setting. The first request after a restart results in the exception: [ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Http.HttpMessageInvoker'.] System.Net.Http.HttpMessageInvoker.CheckDisposed() +327456 System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) +24 System.Web.Http.Owin.<InvokeCore>d__0.MoveNext() +501 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99 System.Runtime.CompilerServices.TaskAwaiter

Referencing Action Parameters from ExceptionLogger

☆樱花仙子☆ 提交于 2019-11-30 19:22:56
I'm wanting to make use of the new method for globally logging errors. I've written a class that inherits ExceptionLogger and overrides the Log() method. Then registered it as a replacement. public class TraceExceptionLogger : ExceptionLogger { public async override void Log(ExceptionLoggerContext context) { // This is always empty string var content = await context.Request.Content.ReadAsStringAsync(); // This is almost always null var actionContext = context.ExceptionContext.ActionContext; } } I can dig through the ExceptionLoggerContext object's properties to get pretty much everything I

Load JSON string to HttpRequestMessage

本秂侑毒 提交于 2019-11-30 18:22:32
I'm writing some tests for my WebAPI web service and cannot figure out how to send JSON to my service method in the test. ScheduleRequest sr = new ScheduleRequest(); sr.Months = null; sr.States = null; sr.Zip = null; sr.Miles = null; sr.PCodes = null; sr.PageStart = 1; sr.PageLimit = 10; HttpRequestMessage m = new HttpRequestMessage(); string sr_ = JsonConvert.SerializeObject(sr); // How do I load it into the HttpRequestMessage??? // m.Content. = sr_; var controller = new ShoppingCartController(); // Call the controlelr method and test if the return data is correct. EventSyncResponse res =