asp.net-web-api2

Web API method not returning the response

允我心安 提交于 2019-12-08 09:50:31
问题 I have a requirement to use Web API. I have implemented Add functionality successfully , and now in the Edit . I am calling the PUT method from my mvc controller thorough and intermediate class and which returns return Request.CreateResponse(HttpStatusCode.OK); This is the return from my PUT method public HttpResponseMessage SampleEdit(int sampleId, Sample sample) { string uri = baseUri + "Sample/" + sampleId; using (HttpClient httpClient = new HttpClient()) { Task<HttpResponseMessage>

ASP.NET Web API 2 cannot return stream when NET Standard 1.6 project is referenced

对着背影说爱祢 提交于 2019-12-08 07:40:39
问题 This is one of the weird issue I have seen and here are the steps to reproduce 1> Create new Web API project in VS 2017 with Target Framework 4.6.2 Create new ASP.NET Web Application -> Select Empty -> Check Web API -> Click OK 2> Add Controller that returns a Stream public class ReportController : ApiController { [HttpGet] public HttpResponseMessage Download() { var filePath = "C:\\Report.xlsx"; var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); var result = new

Global Exception Handler in Web API 2.2 is not catching an exception

百般思念 提交于 2019-12-08 07:21:17
问题 I have a WebForms website project in .Net 4.5 to which I have added ASP.Net Web API 2.2 NuGet package. The Web API methods are being called successfully and returning expected values. However, after I created a global exception handler class for Web API 2.2, and changed the controller class name from ProductController to Product to force the following error, then this error was not getting caught by global exception handler. Error Message when controller suffix is removed from Web API class

Override error code on AuthorizationCodeProvider.Create

故事扮演 提交于 2019-12-08 06:25:58
问题 This question is about the implementation of the Authorization Code flow using Owin in Asp.net Wep Api. I was trying to handle some error that might happen on my AuthorizationCode code creation. Apparently I can't redirect my self to the Client Redirect URI with he correct error code which is "server_error" The following is my code : private static void CreateAuthorizationCode(AuthenticationTokenCreateContext context) { try { //Some Code to create and save the AuthorizationCode that can throw

Getting Error <location allowOverride=“false”> while hosting WEB API 2 in Hostgator

﹥>﹥吖頭↗ 提交于 2019-12-08 06:20:42
问题 I am using WEB API 2 its working in my local system,but when WEB API 2 run in Hostgator windows server its given me error below is my error screenshot http://prntscr.com/bc5ihv 回答1: As I commented, I got similar problem while hosting web api on shared hosting. The solution is to remove Microsoft.CodeDom.Providers.DotNetCompilerPlatform package and clean and build You can deploy even without Roslyn with no change in code. Open NuGet Package Manager window and uninstall Microsoft.CodeDom

Getting Error <location allowOverride=“false”> while hosting WEB API 2 in Hostgator

跟風遠走 提交于 2019-12-08 05:46:27
I am using WEB API 2 its working in my local system,but when WEB API 2 run in Hostgator windows server its given me error below is my error screenshot http://prntscr.com/bc5ihv As I commented, I got similar problem while hosting web api on shared hosting. The solution is to remove Microsoft.CodeDom.Providers.DotNetCompilerPlatform package and clean and build You can deploy even without Roslyn with no change in code. Open NuGet Package Manager window and uninstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform package and rebuild & republish. This uninstallation also removes CodeDom

Get custom validation error message from Web Api request to HttpClient

岁酱吖の 提交于 2019-12-08 04:51:07
问题 I have the following model for my ASP.NET Web Api 2 service: public class Message { public int Id { get; set; } [Required] [StringLength(10, ErrorMessage = "Message is too long; 10 characters max.")] public string Text { get; set; } } I am making the request from my WinForms app: using (var client = new HttpClient()) { client.BaseAddress = new Uri(BaseAddress); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application

Unit Test Web API - How to get auth token

放肆的年华 提交于 2019-12-08 04:41:14
问题 I use token auth for my WebApi application. I have the following ConfigureAuth method in Startup class: // Configure the application for OAuth based flow PublicClientId = "self"; OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new ApplicationOAuthProvider(PublicClientId), AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), // In production mode set

Rest API authentication mechanism, what to do

Deadly 提交于 2019-12-08 04:37:30
问题 I've been reading a lot lately about WEB API authentication mechanisms and I'm a little bit confused regarding how to implement my Web API authentication mechanism, I'm thinking on using Token based authentication but I'm not sure if it is the right choice. Basically my Web API will manage all the operations needed and it will store the users of my website as well the API users(in case they have to be separated). I want to support the following User can register on my website and apps using

Best way to handle unauthenticated request in asp.net Web Api

五迷三道 提交于 2019-12-08 04:34:30
问题 I have a standard VS2013 MVC5 project with a Web Api 2 in it. The way the standard project is designed, the [Authorize] attributes simply return a 401 status code if the request is not authenticated, while a totally separate module sniffs for any 401 codes, halts them, and instead sends a 302 redirect to the login page specified in the Startup.Auth.cs file. That's ok for Mvc controllers, but really stinks for Web Api controllers because for example browsers will automatically redirect ajax