asp.net-core-webapi

Unit test controller model validation on AspNetCore

假装没事ソ 提交于 2019-12-01 06:33:40
In an ASPNET Core project I am trying to create some unit tests that would verify my data validation logic works fine. My controller is very simple: [HttpPost] [Route("Track")] public void Track([FromBody] DataItem item) { if (!ModelState.IsValid) throw new ArgumentException("Bad request"); _dataItemSaver.SaveData(item); } I am using a test base class that would set up the _myController object as the controller under test. public ControllerTestBase() { var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"buildversion

How to redirect to action in ASP.NET Core WebAPI?

无人久伴 提交于 2019-12-01 06:16:10
问题 I've got two actions in my ASP.NET Core Web API application's controller: [HttpGet("get-all")] public IActionResult GetAll() { ... } and [HttpDelete("{id}")] public IActionResult Delete(int id) { ... return RedirectToAction("GetAll"); } Delete action always redirects to itself and never to GetAll . Why so? In the same time similar redirect from Post action works ok. Can't find any docs on the subject. Any help? 回答1: Have you tried to use RedirectToActionResult? Like this (change

Angular JS MVC Web API Model/ Parameter not binding .NET Core

假如想象 提交于 2019-12-01 04:43:14
I am using Angular JS with TypeScript and ASP.NET Core MVC/API. I have an apiService which deals with all POST and GET requests to the server, which looks like this: module TBApp { export class apiService { static $inject = ['$http', 'notificationService']; constructor(private $http, private notificationService: notificationService) { } get(url, config, success, failure) { return this.$http.get(url, config) .then(result => { this.handleResponse(result, success); }, result => { this.handleError(result, failure) }); } post(url, data, success, failure) { return this.$http.post(url,data) .then

ASP.Net Core Web API convention-based routing?

左心房为你撑大大i 提交于 2019-12-01 03:25:31
What am I missing that I'm greeted with a 404 for this controller? I really don't want to use attribute-based routing. I also don't want action to be part of any URIs. I'm using Visual Studio 2017 and .Net Core 1.1. TestController.cs using System; using Microsoft.AspNetCore.Mvc; namespace Foo.Controllers { public class TestController : Controller { public long Get() => DateTimeOffset.Now.ToUnixTimeSeconds(); } } Note that this works with a [Route("api/Test")] attribute. But I don't want to use attribute-based routing. And as soon as I take that attribute off, I get 404s. Startup.cs namespace

ASP.Net Core Web API convention-based routing?

北慕城南 提交于 2019-12-01 00:19:58
问题 What am I missing that I'm greeted with a 404 for this controller? I really don't want to use attribute-based routing. I also don't want action to be part of any URIs. I'm using Visual Studio 2017 and .Net Core 1.1. TestController.cs using System; using Microsoft.AspNetCore.Mvc; namespace Foo.Controllers { public class TestController : Controller { public long Get() => DateTimeOffset.Now.ToUnixTimeSeconds(); } } Note that this works with a [Route("api/Test")] attribute. But I don't want to

What's the point of hosting.json since appsettings.json is sufficient

让人想犯罪 __ 提交于 2019-11-30 22:30:44
In .NET Core 2 Web API app, I could override configuration urls using appsettings.json , but in the official docs they introduced extra file "hosting.json", Why? What's the point of adding complexity? Below code is fully working using appsettings.json : public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) { var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) //see Side note below .AddJsonFile("appsettings.json", optional: true) .AddCommandLine(args) .Build(); return

post multipart/form-data in c# HttpClient 4.5

╄→尐↘猪︶ㄣ 提交于 2019-11-30 20:07:46
Problem I am trying to post API to send data to API which calls my internal API service to send that data to other API i service. Entity contains property with files . this send only file to the other derive but the NameSender property not send with the file. Entity public class Email { public string NameSender{ get; set; } public List<IFormFile> Files { get; set; } } Api [Consumes("multipart/form-data")] [HttpPost] public IActionResult SendEmail([FromForm]Entity entity) { try { string Servicesfuri = this.serviceContext.CodePackageActivationContext.ApplicationName + "/" + this.configSettings

.Net Core 2.0 Database First Approach Scaffold-DbContext of Mysql DB

ぐ巨炮叔叔 提交于 2019-11-30 19:30:31
I am developing WEB API in .Net Core 2.0 with MySQL DB. I am trying to scaffolding MySQL DB. I have follow This link (MySQL Official Site) but when I fire scaffolding command I getting error both I have mention below, please do let me know if I am doing anything wrong. Command For Scaffolding(firing in Package Manager Console) Scaffold-DbContext "server=localhost;port=3306;user=root;password=darshan7826;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir sakila -f Error on executing above command System.NotImplementedException: The method or operation is not implemented. at MySql.Data

Accept x-www-form-urlencoded in Web API .Net Core

前提是你 提交于 2019-11-30 19:24:34
I have a .Net Core Web API that is returning a 415 Unsupported Media Error when I try to post some data to it that includes some json. Here's part of what is returned in the Chrome Debugger: Request URL:http://localhost:51608/api/trackAllInOne/set Request Method:POST Status Code:415 Unsupported Media Type Accept:text/javascript, text/html, application/xml, text/xml, */* Content-Type:application/x-www-form-urlencoded action:finish currentSco:CSharp-SSLA:__How_It_Works_SCO data:{"status":"incomplete","score":""} activityId:13 studentId:1 timestamp:1519864867900 I think this has to do with my

Access Raw Request Body

谁说胖子不能爱 提交于 2019-11-30 19:00:48
I'm trying to access a request's raw input body/stream in ASP.net 5. In the past, I was able to reset the position of the input stream to 0 and read it into a memory stream but when I attempt to do this from the context the input stream is either null or throws an error (System.NotSupportedException => "Specified method is not supported."). In the first example below I can access the raw request in a controller if I declare the controller method's parameter object type as dynamic. For various reasons, this is not a solution and I need to access the raw request body in an authentication filter