asp.net-web-api

How to get UserID from ASP.NET Identity when sending Rest Request

允我心安 提交于 2021-02-10 14:16:26
问题 I have an API which uses ASP.NET Identity, it is fairly easy for me to get the UserId once the token has been generated as following HttpContext.Current.User.Identity.GetUserId().ToString()) Now I have an external application which is trying to authenticate using this API but I need the UserId of the user who generated the token When I send a request to http://myapiURL/token I get the following access_token token_type expires_in userName issued expires And when I send a request to get API

How do I set or remove the Default Response Content Type Using SwashBuckle

被刻印的时光 ゝ 提交于 2021-02-10 12:56:57
问题 The default response content type when using SwashBuckle is text/plain . How can I change the default to be application/json or even remove text/plain ? 回答1: The response content of an end point is not determined by Swashbuckle but by the formatters set in the configuration of your ASP.NET Web API project. To remove the text/plain content type and only support application\json you could add this to the Register method of your WebApiConfig : GlobalConfiguration.Configuration.Formatters.Clear()

How do I set or remove the Default Response Content Type Using SwashBuckle

ε祈祈猫儿з 提交于 2021-02-10 12:56:15
问题 The default response content type when using SwashBuckle is text/plain . How can I change the default to be application/json or even remove text/plain ? 回答1: The response content of an end point is not determined by Swashbuckle but by the formatters set in the configuration of your ASP.NET Web API project. To remove the text/plain content type and only support application\json you could add this to the Register method of your WebApiConfig : GlobalConfiguration.Configuration.Formatters.Clear()

DbContext.SaveChangesAsync Exception Handling

不羁岁月 提交于 2021-02-10 07:26:03
问题 When scaffolding a new ApiController with asynchronous actions and Entity Framework support in Visual Studio 2013, some methods wrap DbContext.SaveChangesAsync calls in try-catch blocks. For instance, the Put method, try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return NotFound(); } throw; } From msdn.microsoft.com about DbUpdateConcurrencyException , Exception thrown by DbContext when it was expected that SaveChanges for an entity

DbContext.SaveChangesAsync Exception Handling

若如初见. 提交于 2021-02-10 07:23:10
问题 When scaffolding a new ApiController with asynchronous actions and Entity Framework support in Visual Studio 2013, some methods wrap DbContext.SaveChangesAsync calls in try-catch blocks. For instance, the Put method, try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return NotFound(); } throw; } From msdn.microsoft.com about DbUpdateConcurrencyException , Exception thrown by DbContext when it was expected that SaveChanges for an entity

.net Core 2.0 web api 400 error using Validateantiforgerytoken

ぃ、小莉子 提交于 2021-02-10 06:19:12
问题 I have two .Net Core applications, a Web API and Client. Sending Post from the client using : <form asp-action="Create" method="post"> @Html.AntiForgeryToken() ..... </form> Client controller: public async Task<IActionResult> Create([Bind("QuestionId,TheQuestion")] SecurityQuestion securityQuestion) { _session.SetString(SessionsKeys.Directory, "/SecurityQuestions"); if (ModelState.IsValid) { var data = await _theService.PostWebApi(securityQuestion); if (data.Item3 == "True") { return

The type or namespace 'Http' doesn't exist in the namespace 'System.Web' in Web API

独自空忆成欢 提交于 2021-02-10 04:41:49
问题 I'm trying to create a Web API following this link . I have exactly added the way it is shown in this article. But after adding the controller, it is giving me this error for System.Web.Http and ApiController . Error message on System.Web.Http : The type or namespace Http doesn't exist in the namespace 'System.Web' Error message on ApiController: The type or namespace ApiController could not be found. Additional Information : I'm using Visual Studio 2015 Professional. Framework : .NET

Problems Self-Hosting ASP.NET Web API in a Windows Service Application

好久不见. 提交于 2021-02-10 03:20:49
问题 I've seen some articles on the web describing how to self host an ASP.NET Web API in a Windows service application (see here and here). I've written some simple code in VB.NET to start the self host when the service starts and stop it when the service stops, like so: Protected Overrides Sub OnStart(ByVal args() As String) Try ' init a new self host configuration using the base address _config = New HttpSelfHostConfiguration(New Uri("http://localhost:8080")) ' map the URLs into the config

WebAPI - Enable CORS for Subdomains

左心房为你撑大大i 提交于 2021-02-08 15:01:28
问题 I would like to be enable CORS for a web-api application with the following criteria: Allow HTTPS or HTTP for the same site Ignore the SUBDOMAIN - Meaning mysite.com and www.mysite.com are same I would like to do this for multiple sites in an elegant way, rather than putting all the permutations as comma delimited. Thanks in advance! 回答1: Here you go. Adding Git gist if it requires any revisions or bug fixes. public class WildcardOriginCorsPolicy : Attribute, ICorsPolicyProvider { private

The maximum size of object that can be passed as Parameter to POST method

◇◆丶佛笑我妖孽 提交于 2021-02-08 14:38:12
问题 I have a web API controller with a POST method as follows. public class MyController : ApiController { // POST: api/Scoring public HttpResponseMessage Post([FromBody]MyClass request) { // some processing of request object return Request.CreateResponse(HttpStatusCode.OK, someResponseObject); } .... } This is consumed by a HTTPClient as follows HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));