asp.net-core-mvc

Unit test for a collection of anonymous JSON objects

放肆的年华 提交于 2019-12-01 20:54:45
This question was inspired by this excellent example . I have ASP.NET Core MVC application and I am writing unit tests for the controller. One of the methods returns JsonResult with a collection of anonymous types. I can get to each element of the collection. I can also assert values in each element like this: Dictionary<int, string> expectedValues = new Dictionary<int, string> { { 1, "Welcome Tester"}, { 2, "Namaste Tester"}, { 3, "Privet Tester"}, { 4, "Labdien Tester"} }; foreach (dynamic value in jsonCollection) { dynamic json = new DynamicObjectResultValue(value); Assert.Equal

How do I read a local file in my ASP.NET Core project

拈花ヽ惹草 提交于 2019-12-01 20:42:29
If I want to read a local file from my project, given a relative path from the project directory, how could I achieve this? You can get the ApplicationBasePath from PlatformServices.ApplicationEnvironment . This will make it possible to resolve non-absolute paths relative to your application base path. Now that .Net Core has RTM-ed you need to use IHostingEnvironment instead. It exposes WebRootPath and ContentRootPath 来源: https://stackoverflow.com/questions/35877583/how-do-i-read-a-local-file-in-my-asp-net-core-project

What do I do when ASP.NET 5 (vNext) can't redirect bindings?

我的梦境 提交于 2019-12-01 18:27:13
I am just getting my feet wet with MVC 6. I installed VS 2015 and with the default ASP.NET 5 preview MVC Web Application template everything runs fine under local IIS. I then tried to switch out the Default DI container with StructureMap following these instructions exactly (note it is a very recent article). The only thing is I had to figure out the namespaces to import myself (since the author neglected to include them) and this is what I included. I put the StructureMapRegistration class and all related classes into a single file, and here are the usings. using Microsoft.Framework

How can I call a controller action when rendering a partial view?

荒凉一梦 提交于 2019-12-01 18:22:22
I am creating a partial view for a sidebar that will show the most popular posts in my site. How can I create a separated controller for loading the model required by the partial view? (The IEnumerable<Post> with the popular posts) Currently I have created a controller class that loads the popular posts but I keep getting errors when rendering the partial as I cannot call the controller and load the partial model. For example, if I call it from a view where I render a single post, the model types won't match ( Post vs IEnumerable<Post> ) This is my SidebarController : using System; using

How can I bind an array with asp-for tag?

天涯浪子 提交于 2019-12-01 18:04:50
I would like to ask, how can I bind an array in Asp.NET Core MVC ? <input type="text" asp-for="Requests[@index].Name" /> It was working very well in older versions of ASP MVC. This example shows "Internal server error". " An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately. " ViewModel class example: public class ViewModel { public List<Model> Requests {get;set;} } Model class example: public class Model { public string Name {get;set;} } How it should work ? After

How to access IConfigurationRoot in startup on .net core 2?

别说谁变了你拦得住时间么 提交于 2019-12-01 17:53:17
I have written a custom ConfigurationProvider with the entity framework. Since I also want to make it updateable during runtime, I have created a IWritableableOption . I need to refresh the configuration after the update. This can be done via IConfigurationRoot.Reload . However, how can I get the IConfigurationRoot in .net core 2? What I have found, is that in previous versions the IConfigurationRoot was part of startup. In .net core 2 however, we have only the simpler type IConfiguration : public Startup(IConfiguration configuration) { // I tried to change this to IConfigurationRoot, // but

How to use HTML links in ASP.Net Core MVC?

风流意气都作罢 提交于 2019-12-01 17:49:37
问题 I'm currently trying to make a website in ASP.NET Core MVC. In my layout page, I'm making a navigation bar to access all of the actions that can be reached through my controllers. I am however unable to create useful links. <ul> <li><a href="homepage">Home</a></li> <li><a href="index">Index</a></li> </ul> My problem with this is that I still need the controller before the links and if I put the controller in front of the action like this <ul> <li><a href="home/homepage">Home</a></li> <li><a

How to use HTML links in ASP.Net Core MVC?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 17:44:00
I'm currently trying to make a website in ASP.NET Core MVC. In my layout page, I'm making a navigation bar to access all of the actions that can be reached through my controllers. I am however unable to create useful links. <ul> <li><a href="homepage">Home</a></li> <li><a href="index">Index</a></li> </ul> My problem with this is that I still need the controller before the links and if I put the controller in front of the action like this <ul> <li><a href="home/homepage">Home</a></li> <li><a href="home/index">What We've Done</a></li> </ul> When I click on one link and then the other, the link

ASP.NET Core - Changing form values after a post

北慕城南 提交于 2019-12-01 17:41:56
问题 I'm probably missing something very obvious, but all I'm trying to do is have a simple form, allow it to POST, change one of the fields, and when it returns it should show me the changed value. Simple, right? The model looks like this: public class TestModel { public string Field1 { get; set; } public string Field2 { get; set; } } The controller actions: public IActionResult Test() { return View(new TestModel()); } [HttpPost] public IActionResult Test(TestModel model) { model.Field1 =

How to update appsettings.json based on publish profile using .NET Core?

て烟熏妆下的殇ゞ 提交于 2019-12-01 17:34:06
I'm currently making the switch from .NET Framework to .NET Core. In the past, all of my application settings were in the Web.config file. When I added a new publish profile, I could right click and select 'Add Config Transform' which would generate a nested Web.{Profile}.config file under Web.config where I could set application settings that were specific to the respective profile. Now, in .NET Core, I want to achieve the same effect using an appsettings.json file instead of Web.config file. How can I create an appsettings.{Profile}.json file which will be nested under my appsettings.json