asp.net-web-api2

Web Api 2 - custom data type JSON serialization

落爺英雄遲暮 提交于 2019-12-20 02:38:04
问题 I'm actually new to Web Api, so my question may sound a bit odd. I have simple API to return historical information about price changes. My controller's action looks like this: [HttpGet] [Route("api/history/{id}/{size}")] public async Task<IEnumerable<PriceHistoryRecordModel>> GetHistory(string id, Size size) where PriceHistoryRecordModel is [DataContract] public class PriceHistoryRecordModel { [DataMember] public DateTime Date { get; set; } [DataMember] public double Value { get; set; } }

.NET WebApi breaks when url ends with ampersand

▼魔方 西西 提交于 2019-12-20 01:59:17
问题 I have an ApiController - for example 'Home' controller, with action 'Test' which accepts two parameters - test1 and test2, both with default values [System.Web.Http.HttpGet] public ActionResult Test(int test1 = 3, int test2 = 5) { var a = 0; return null; } Now when I call Home/Test?test1=1 , everything is OK. But when I call Home/Test?test1=1& , the server throws exception The parameters dictionary contains a null entry for parameter 'test2' of non-nullable type 'System.Int32' for method

transfer JWT Authentication implementation from .net core 2 to asp.net web api 2

自古美人都是妖i 提交于 2019-12-19 10:44:14
问题 i have implementation of JWT Authentication in .net core 2 application , it work fine. i want to use this implementation and structure in asp.net web api 2 application but i get error my structure: JwtTokenBuilder class: using System; using System.Collections.Generic; using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Linq; namespace solution.Authentication { public sealed class JwtTokenBuilder { private SecurityKey

Create ASP.NET WEB API using Console application

最后都变了- 提交于 2019-12-19 10:39:07
问题 I have seen many articles in Internet to create RESTFUL webapi by selecting webapi project as project type. Is there any possibility that we can create the same using console application. if possible, how can it be hosted? how Executable file gives us the URL to consume from client application. 回答1: Yes, there is a possibility. but we need to flow some steps to achieve this. we should use OwinSelfHost package for self hosting. we should create as class file that inherits from ApiController.

Error 401 Unauthorized. How to Use the same token for different Urls?

柔情痞子 提交于 2019-12-19 10:25:03
问题 In ASP.Net Identity using Oauth2 a token is created once the user is authenticated posting User and Password. Before making a call to an action from one API, the user must ask for a token: http://mysite/auth/token Once the token is received, all Web Api calls can be done, sending the Authorization: Bearer <token> header: GET http://mysite/auth/product/1 PUT http://mysite/auth/client/42 I have several Web Apis that use a centralised Security System for Authentication, the problem is that I

ServicePointManager.DefaultConnectionLimit returning Int32.MaxValue

十年热恋 提交于 2019-12-19 10:16:27
问题 For diagnostics purposes I am logging ServicePointManager.DefaultConnectionLimit. However oddly enough it seems to be returning Int32.MaxValue (i.e 2147483647). This seem to contradict the MSDN documentation on the subject: The maximum number of concurrent connections allowed by a ServicePoint object. The default value is 2. For context, I am getting this value in an ASP.Net 4 application running on 4.6.1 回答1: Based on @Wimmel's link it seems in ASP.Net that it is set to Int32.MaxValue as

Get the IP address of the client connecting to a C# .NET WebAPI application

我与影子孤独终老i 提交于 2019-12-19 09:16:25
问题 I tried: private const string HttpContext = "MS_HttpContext"; private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; public static string GetClientIpAddress(HttpRequestMessage request) { if (request.Properties.ContainsKey(HttpContext)) { dynamic ctx = request.Properties[HttpContext]; if (ctx != null) { return ctx.Request.UserHostAddress; } } if (request.Properties.ContainsKey(RemoteEndpointMessage)) { dynamic remoteEndpoint = request

Configure request timeout for WebApi controllers

有些话、适合烂在心里 提交于 2019-12-19 05:51:28
问题 I'm using async methods in my WebAPi controllers: public async Task<HttpResponseMessage> SampleMethod(int subscriptionNumber, DateTime departureDate) { // [...] } How do I configure the request timeout? The operation can take up to a couple of minutes and I have to make sure that the request do not timeout. In MVC there is an attribute called [AsyncTimeout] . Are there an equivalent in WebApi? Can it be configured globally? 回答1: Good question, I would recommend to handle this from client side

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

[亡魂溺海] 提交于 2019-12-19 04:37:20
问题 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. 回答1: 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

How to Instantiate ODataQueryOptions

谁都会走 提交于 2019-12-19 03:41:23
问题 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