asp.net-web-api

How to read/parse Content from OkNegotiatedContentResult?

两盒软妹~` 提交于 2020-01-14 07:16:11
问题 In one of my API actions ( PostOrder ) I may be consuming another action in the API ( CancelOrder ). Both return a JSON formatted ResultOrderDTO type, set as a ResponseTypeAttribute for both actions, which looks like this: public class ResultOrderDTO { public int Id { get; set; } public OrderStatus StatusCode { get; set; } public string Status { get; set; } public string Description { get; set; } public string PaymentCode { get; set; } public List<string> Issues { get; set; } } What I need is

Using optional complex type action parameter for a POST Web API

 ̄綄美尐妖づ 提交于 2020-01-14 07:15:12
问题 I am trying to write an API with the following prototype: [HttpPost] public ApplicationStatus appLogIn(string id, UserCredentials userCredentials = null) But when I make a request to this API, with or without userCredentials , I get the following error with response code as 500 { "Message": "An error has occurred.", "ExceptionMessage": "Optional parameter 'userCredentials' is not supported by 'FormatterParameterBinding'.", "ExceptionType": "System.InvalidOperationException", "StackTrace":

What does HttpResponseMessage return as Json

百般思念 提交于 2020-01-14 07:15:09
问题 I have a basic question about basics on Web Api. FYI, I have checked before but could not found what I was looking for. I have a piece of code as described below these lines. Just like any other Method in general terms my method called: Post, it has to return something,a JSON for example, How do I do that. Specifically, what am I supposed to write after the word " return " in order to get the 3 fields( loginRequest.Username,loginRequest.Password,loginRequest.ContractItemId ) as Json. Coments:

What does HttpResponseMessage return as Json

北慕城南 提交于 2020-01-14 07:15:06
问题 I have a basic question about basics on Web Api. FYI, I have checked before but could not found what I was looking for. I have a piece of code as described below these lines. Just like any other Method in general terms my method called: Post, it has to return something,a JSON for example, How do I do that. Specifically, what am I supposed to write after the word " return " in order to get the 3 fields( loginRequest.Username,loginRequest.Password,loginRequest.ContractItemId ) as Json. Coments:

How to change controller's name in swagger-ui?

↘锁芯ラ 提交于 2020-01-14 06:49:08
问题 if I have the following: MySimpleTestController : ApiController { } Is it possible to have the controller name appear as "My Simple Test" instead of "MySimpleTest" in the generated API docs? I did a search but I mostly found ways to do it in Java using an @Api annotation but I am using C# (.net 4.5.2). Here is one way to do it but it requires adding an annotation to every single route in the Controller which would be cumbersome as my controller has many routes. Is there a better way? 回答1:

MVC Web API Error Code SCRIPT7002

筅森魡賤 提交于 2020-01-14 05:20:51
问题 For reference I'm using Windows 10 and Visual Studio 2017. I have an MVC web api and corresponding web application complete with login capabilities. I'm simply attempting to debug using IE. Whenever I use a GET call I'm met with the following error: SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied. I have CORS configured and enabled so I'm at a complete loss as to why this isn't working. In my WebApiConfig file I have this: public static void Register(HttpConfiguration

Identity server 4 token not validate in .NetFramework Api that use Identity Server 3

浪尽此生 提交于 2020-01-14 04:32:08
问题 In my identityserver app that use idsv4 and run on port "5000" have a client new Client { ClientId = "client", // no interactive user, use the clientid/secret for authentication AllowedGrantTypes = GrantTypes.ClientCredentials, // secret for authentication ClientSecrets = { new Secret("secret".Sha256()) }, // scopes that client has access to AllowedScopes = { "api1" } }` and in my .Net Framework Api's startup class that use port no "7001" : app.UseIdentityServerBearerTokenAuthentication( new

Hosting WebAPI in Windows-Service crashes on first attempt

牧云@^-^@ 提交于 2020-01-14 04:16:49
问题 I have an console-app with hosted WebAPI (HttpSelfHostServer). In the console-app I have Controllers that query data from SQL-Server. The console-app works without problems (from fiddler and a Windows_client) Now I want to host the WebAPI (HttpSelfHostServer) in a Windows-Service. Therefore I have created a Service-Project and referenced the Controllers in console-app-dll so that the controllers should be found. To have a minimal chance to debug, I write some information in the event log from

ASP.NET WebApi Changing default parameter binding error message

╄→尐↘猪︶ㄣ 提交于 2020-01-14 04:03:10
问题 I have action method defined as public HttpResponseMessage Get(SomeEnum? param) { ... } If I pass some invalid value for param which is not convertible to Some Enum type I get this message: The value 'xxx' is not valid for Nullable`1. It is default message which I can get from ModelState. I would like to customize this message. I have found plenty of tips how to do it in ASP.NET MVC (like here) but nothing for WebAPI. Changing DefaultModelBinder.ResourceClassKey does not work in WebAPI. I

Web Api not converting json empty strings values to null

送分小仙女□ 提交于 2020-01-14 04:00:09
问题 Example Json: {"Field1":"","Field2":null}. In MVC, Field1 would be converted to null by default. I tried the [DisplayFormat(ConvertEmptyStringToNull = true)] attribute, (which should be the default anyway) and it did not make a difference. I'm using Web Api 2.1 Any ideas? 回答1: in C# an empty string is not the same as a null reference, and json.NET which is the underlying json implementation decided to avoid automatic conversions. You can add the following custom converter to deal with that