asp.net-web-api2

IFormFile is always null when receiving file from console app

梦想的初衷 提交于 2019-12-06 11:41:26
问题 I have my WebApi method ready to accept file as parameter shown below: (e.g. uri "https://localhost:44397/api/uploadfile" [Route("api/[controller]")] [ApiController] public class UploadFileController : ControllerBase { [HttpPost] public async Task<IActionResult> Post([FromForm] IFormFile file) { } } Am using console app to send file to this api method, below is my code: public static void Send(string fileName) { using (var client = new HttpClient()) using (var content = new

With Azure App Service Easy Auth + Azure AD B2C is it possible to secure a single Web API and have multiple native apps consume it?

有些话、适合烂在心里 提交于 2019-12-06 11:30:48
We have a Web API intended to serve multiple business partners, each of which will be customizing a white label version of our native app client. We also have a Web API offering common functions to different apps. We would like to use AD B2C as the identity and auth system, but cannot see how or if it is possible to use AD B2C to secure a common API for multiple apps. Is this achievable? It depends on how you want to your partners usig the account login-in. If you expected that the partners login-in using the consumer account or local account in the Azure AD B2C you own, the answer is yes. The

Uploading image in ASP.NET Boilerplate

女生的网名这么多〃 提交于 2019-12-06 11:20:34
When posting an image, HttpContext.Current.Request is null . Is there any simple way to achieve this? I am using dropzone.js on client side. Project is Angular with Web API (ASP.NET Core 2.0) template. [HttpPost] public HttpResponseMessage UploadJsonFile() { HttpResponseMessage response = new HttpResponseMessage(); var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; var filePath = HttpContext.Current.Server.MapPath("~/UploadFile/" + postedFile.FileName); postedFile.SaveAs

OWIN OAuth 2.0 - Bearer Token Never Expire

被刻印的时光 ゝ 提交于 2019-12-06 11:09:04
问题 I'm using the following OAuth provider and options: UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new ApplicationDbContext())); OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory), AuthorizeEndpointPath = new PathString("/api/AccountOwin/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(2), AllowInsecureHttp = true };

Unit test WebApi2 passing header values

萝らか妹 提交于 2019-12-06 10:53:38
问题 I am working on a project using WebApi2. With my test project I am using Moq and XUnit. So far testing an api has been pretty straight forward to do a GET like [Fact()] public void GetCustomer() { var id = 2; _customerMock.Setup(c => c.FindSingle(id)) .Returns(FakeCustomers() .Single(cust => cust.Id == id)); var result = new CustomersController(_customerMock.Object).Get(id); var negotiatedResult = result as OkContentActionResult<Customer>; Assert.NotNull(negotiatedResult); Assert.IsType

MVC Web API: dot in URL parameter value

只愿长相守 提交于 2019-12-06 10:49:12
I have implemented search functionality to look for data with a (part of a) code as search criteria, which includes a . (dot) in the value, so it should be possible to include that in the search criteria. Consider url: myhost/api/search/88. Out of the box, without doing anything extra, that will result in a 404 error. While, not surprisingly, the url works fine if I remove the dot. I found this as possible answer on StackOverflow: <system.web> <httpRuntime relaxedUrlToFileSystemMapping="true" /> </system.web> Question This does the trick, but I am not sure if this is the best solution. I mean,

Removing NaN values in deserialization Web API 2 C#

喜欢而已 提交于 2019-12-06 09:59:24
Hello I am wondering if anyone could help me, I am trying to automatically replace NaN from double values automatically with 0 upon deserializing automatically in Web API 2. I am trying to use JSON.NET but I am having no success. Any help would be greatly appreciated. I am placing the below into my WebApiConfig config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); MediaTypeHeaderValue appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); config.Formatters.XmlFormatter.SupportedMediaTypes

Multiple types were found that match the controller

醉酒当歌 提交于 2019-12-06 09:49:17
I'm trying to api versioning using header but in different folder structure like below. In Controller folder have V1 sub folder inside that CustomerController.cs and In Controller folder have V2 sub folder inside that CustomerController.cs when I user api version using URL above works fine. my issue is When I try this approach with header it is giving me below error: { "Message": "An error has occurred.", "ExceptionMessage": "Multiple types were found that match the controller named 'customer'. This can happen if the route that services this request ('api/{controller}/{id}') found multiple

The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'handler' is not null

一笑奈何 提交于 2019-12-06 09:07:54
问题 I have a custom message handler that I add globally as follows: public class CustomerHandler : DelegatingHandler { public CustomHandler(HttpConfiguration httpConfiguration) { InnerHandler = new HttpControllerDispatcher(httpConfiguration); } } config.MessageHandlers.Add(new CustomHandler(config)); However, I get the following error: The 'DelegatingHandler' list is invalid because the property 'InnerHandler' of 'CustomHandler' is not null. Parametername: handlers So I changed the code to:

.NET Core - Trying to add a repository to my API controller, but when I do every controller method returns a 500 error

好久不见. 提交于 2019-12-06 08:41:10
As the title says. I'm creating a Web API and in my API controller, I'm trying to declare a repository in the constructor. I successfully declare it, but every API method I try to call in that controller returns a 500 error. When I remove the constructor/repository variable, I have no issues. Controller [Route("api/[controller]")] public class TestController: Controller { private ITestRepository _testRepository; public TestController(ITestRepository testRepository) { _testRepository= testRepository; } [HttpGet] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } }