asp.net-core-mvc

Recursion in ASP.NET Core Razor views

删除回忆录丶 提交于 2019-12-07 00:13:21
问题 I have the following code right now to write a flat list of items with a link to a controller action: <ul> @foreach (var item in items) { <li> <a asp-controller="Home" asp-action="Demo" asp-route-itemName="@item.Name"> @item.Name </a> </li> } </ul> Now this must become recursive. Items can also contain subitems. For recursion I need some sort of function. I know I could use @functions and define the function in the .cshtml file. Not sure whether such nice inline HTML code with tag helpers

Base View Page in ASP.NET MVC 6

落花浮王杯 提交于 2019-12-06 23:49:27
问题 On ASP.NET MVC 5 I used a base ViewPage with a few properties: public String PageTitle { get; set; } public String PageDescription { get; set; } public String[] BodyCssClasses { get; set; } Then on each view I would have: @{ PageTitle = "Title ..." PageDescription" = "Description ..." BodyCssClasses = new String[] { "main", "home" } } On the master page I would simply use something like: <title>@Title</title> With this approach I was able to use Strong Typed for page properties ... Is it

ASP.NET Core MVC application insights stopped working after upgrade to .NET Core 1.1.1

限于喜欢 提交于 2019-12-06 23:47:34
问题 I have one simple ASP.NET Core application I was working on back in December 2016. It worked just fine with application insights and telemetry. Now after 4 months I wanted to pick up this work and started with upgrade from .NET Core 1.1.0 to 1.1.1. In this process package Microsoft.ApplicationInsights.AspNetCore got updated from version 1.0.2 to version 2.0.0 . This unfortunately caused my app to stop working, in particular I get this error: An error occurred during the compilation of a

MVC 6 Web Api: Resolving the location header on a 201 (Created)

纵饮孤独 提交于 2019-12-06 21:55:17
问题 In Web Api 2.2, we could return the location header URL by returning from controller as follows: return Created(new Uri(Url.Link("GetClient", new { id = clientId })), clientReponseModel); Url.Link(..) would resolve the resource URL accordingly based on the controller name GetClient : In ASP.NET 5 MVC 6's Web Api, Url doesn't exist within the framework but the CreatedResult constructor does have the location parameter: return new CreatedResult("http://www.myapi.com/api/clients/" + clientId,

How to log ASP.NET Core input JSON serialization errors

耗尽温柔 提交于 2019-12-06 21:16:45
问题 We have ASP.NET Core application which we use as a REST api. Methods use [FromBody]MyClass param as input. Problem is if the client sends an invalid JSON string the input value becomes null due to JSON serialization errors. Is there a way to log such errors? Edit: I'm looking for application-wide solution not per-method.. 回答1: I've solved this with adding an error handler in Startup.cs: services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Error = (object sender,

ASP.net core MVC 6 Data Annotations separation of concerns

这一生的挚爱 提交于 2019-12-06 19:54:30
I want put the Data Annotations Attribute and the IClientValidatable interface in two seperate assemblies to have separation of concerns. One is called Common and the other Comman.Web. These links explain how it works in MVC 5: Keeping IClientValidatable outside the model layer http://www.eidias.com/blog/2012/5/25/mvc-custom-validator-with-client-side-validation Unfortunately in MVC 6 there is no DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapter) ); How does it work in ASP.net core MVC 6? I use the RC1. In Startup.cs , in

Using System.Net.Mail in ASP NET MVC 6 project

点点圈 提交于 2019-12-06 19:50:18
问题 I have trouble creating a simple mock mail sender within an ASP NET 5 project. Here the method : public static Task SendMail(string Email, string Subject, string Body) { SmtpClient client = new SmtpClient(); client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; client.PickupDirectoryLocation = "C:\\TMP"; MailAddress from = new MailAddress("jane@contoso.com", "Jane " + (char)0xD8 + " Clayton", System.Text.Encoding.UTF8); MailAddress to = new MailAddress(Email); MailMessage

Why is Visual Studio telling me I need to reference System.Private.CoreLib?

烈酒焚心 提交于 2019-12-06 19:19:51
问题 I'm trying out EF Core for the first time and have coded a very simple MVC app to get my feet wet. I am using a method for seeding the database found in the UnicornStore project where they write some code in Startup.cs to migrate the database and then run a seed method. Before they call the seed method, they run this DbContext extension method to check if all migrations have been applied: using System; using System.Linq; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore

how to add configuration for log4net (or any other 3rd party library) in ASP.NET 5.0

早过忘川 提交于 2019-12-06 19:16:47
问题 I was reading Scott Gu's blog on ASP.NET 5.0 features and one of the new feature mentioned in the blog is to use json file as configuration and elimination of Web.config file. I have few questions around that feature. Assume I have following log4net configuration which was previously added to Web.Config in previous version of ASP.NET Config file <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> <log4net debug="true"> <appender name=

.Net Core WebAPI , Unable to post data with POSTMAN , error - 415 Unsupported MediaType

杀马特。学长 韩版系。学妹 提交于 2019-12-06 19:01:09
问题 I am testing my first .net Core WebAPI with Postman unknown media type error is occurring. What am I missing? This is my posting object public class Country { [Key] public int CountryID { get; set; } public string CountryName { get; set; } public string CountryShortName { get; set; } public string Description { get; set; } } This is the webapi controller [HttpPost] public async Task<IActionResult> PostCountry([FromBody] Country country) { if (!ModelState.IsValid) { return BadRequest