asp.net-web-api

How to deal with null HttpContext.Current when using auth filters with async/await?

那年仲夏 提交于 2020-01-05 14:53:40
问题 In one of apis in my controller I need to reference HttpContext.Current. Everything worked fine until I implemented a custom authentication filter. The filter uses async methods with await. However, with the filter in place, the HttpContext.Current is null when it reaches my controller. I'm assuming that this is because my controller executes on a different thread that it used to due to async/await in the filter. If this is the case, how should I access the 'original' context in my controller

How to deal with null HttpContext.Current when using auth filters with async/await?

断了今生、忘了曾经 提交于 2020-01-05 14:53:37
问题 In one of apis in my controller I need to reference HttpContext.Current. Everything worked fine until I implemented a custom authentication filter. The filter uses async methods with await. However, with the filter in place, the HttpContext.Current is null when it reaches my controller. I'm assuming that this is because my controller executes on a different thread that it used to due to async/await in the filter. If this is the case, how should I access the 'original' context in my controller

How to deal with null HttpContext.Current when using auth filters with async/await?

让人想犯罪 __ 提交于 2020-01-05 14:53:03
问题 In one of apis in my controller I need to reference HttpContext.Current. Everything worked fine until I implemented a custom authentication filter. The filter uses async methods with await. However, with the filter in place, the HttpContext.Current is null when it reaches my controller. I'm assuming that this is because my controller executes on a different thread that it used to due to async/await in the filter. If this is the case, how should I access the 'original' context in my controller

NetworkCredential working for POST but not GET

我们两清 提交于 2020-01-05 12:15:24
问题 I'm calling a webAPI web service that uses windows authentication. I'm using this code in my development environment since I am developing from a box that is not on the domain. var req = (HttpWebRequest)WebRequest.Create(url); req.Credentials = new NetworkCredential("username", "password", "domain"); req.Method = WebRequestMethods.Http.Post; //or get This works just fine when I do a post, but when I want to do a get it doesn't work. When i visit the url for the get in a web browser it asks

Web-Api 400 BadRequest when search parameter is blank

僤鯓⒐⒋嵵緔 提交于 2020-01-05 11:57:25
问题 I am using web-api with mvc4 I am making searching functionality, in some cases like if i filter data then remove that textbox value and then press search button, need to show whole listing but in my case showing 400 bad request. as search parameter is blank, i know if search parameter blank then it will throw 400 error with web-api. any one have proper solution then please let me know. data: "CurrPage=" + JsCurrPage + "&PageSize=" + parseInt(pagesize) + "&BuildTypeName=" + $("#BuildTypeName"

Returning binary data with web api

时光总嘲笑我的痴心妄想 提交于 2020-01-05 11:56:21
问题 I have the following controller method which returns a byte array. public async Task<HttpResponseMessage> Get() { var model = new byte[] { 1, 2, 3 }; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(new MemoryStream(model)); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return result; } I think this is an older way of implementing this functionality with web api. Is there a more "modern"

Actual use of GET,PUT,DELETE,POST methods in http

久未见 提交于 2020-01-05 11:01:07
问题 I am using asp.net web API(RESTful service) where I can use post method for delete, insert and update operation by giving some extra parameters. Is it used for only semantic usage of the words that can differentiate functionality?. I found nice SO post Here. There are some doubts as below I can use POST for posting data then why PUT I can delete by POST by passing deleted id then why DELETE What happens at server side when it receives such header methods for each. Please assist for the same.

How does one define an (optional) navigation property (?$expand=) in code?

会有一股神秘感。 提交于 2020-01-05 10:29:28
问题 First the explanation and the gist, then the question. So: Let's say I have a view AccountView defined in EF (6.1.1) database first (edmx) so that the code-generated class is //This class is generated from a view by EF (edmx)... public partial class AccountView { public System.Guid Id { get; set; } public int CompanyId { get; set; } } Then I create a partial class in the same namespace ( Entities ) as [MetadataType(typeof(AccounViewMetaData))] public partial class AccounView { //This is added

Generate Html to Pdf using ItextSharp blank file generate

China☆狼群 提交于 2020-01-05 09:14:00
问题 I am using Itexsharp.dll for generate html to pdf with webapi and angular 2, but I am return bytes array from webapi and open bytes in angular 2 with Blob but when open pdf it was blank. My code is here IN Web api code: - HttpResponseMessage response = new HttpResponseMessage(); MemoryStream workStream = new MemoryStream(); StringReader sr = new StringReader(htmlPdfContent.ToString()); using (MemoryStream memoryStream = new MemoryStream()) { using (Document pdfDoc = new Document(PageSize.A4))

Enable cross-origin requests (CORS) in ASP.NET 5 & MVC 6

不问归期 提交于 2020-01-05 08:23:38
问题 I'm trying to enable CORS in my MVC 6 WebApi project, and I found this question, focusing on the same problem: How do you enable cross-origin requests (CORS) in ASP.NET 5 & MVC 6? It looks exactly like what I want to do, but when I get to this part, I run into trouble. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //Add Cors support to the service services.AddCors(); var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy(); policy.Headers.Add("*"); policy