asp.net-core-mvc

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

风流意气都作罢 提交于 2019-12-05 01:17:16
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 message = new MailMessage(from, to); message.Body = Body; message.BodyEncoding = System.Text.Encoding.UTF8;

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

為{幸葍}努か 提交于 2019-12-05 01:16:38
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="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="C:\\TestProj\\TestLog.txt" />

Not able to redirect to action when using TempData in Asp.Net Core

若如初见. 提交于 2019-12-05 01:05:41
问题 I am trying to achieve a simple thing in Asp.Net Core. This would have been no big deal in Asp.Net Mvc. I have an action method like this public async Task<IActionResult> Create([Bind("Id,FirstName,LastName,Email,PhoneNo")] Customer customer) { if (ModelState.IsValid) { _context.Add(customer); await _context.SaveChangesAsync(); TempData["CustomerDetails"] = customer; return RedirectToAction("Registered"); } return View(customer); } public IActionResult Registered() { Customer customer =

Move Startup.cs to Class Library (Package) Project - ASP.NET 5

落爺英雄遲暮 提交于 2019-12-05 01:03:10
问题 I'm working in a MVC 6 project, and moved my Startup.cs class from the Web Project to an Infraestructure Project, which is a Class Libary Package. I also added the packages required by the class (exactly the same as the Web Project) in Infraestructure/project.son: "Microsoft.AspNet.Diagnostics": "1.0.0-beta8", "Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8", "Microsoft.AspNet.Mvc": "6.0.0-beta8", "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8", "Microsoft.AspNet.Server.Kestrel": "1.0.0

How to consistently get application base path for ASP.NET 5 DNX project on both production and development environment?

丶灬走出姿态 提交于 2019-12-05 00:43:37
I have deployed a ASP.NET MVC 6 website to Azure from Git. Details of the deployment can be found in this blog post but basically I use DNU to publish it and then kudu to push it to an Azure website. Using IHostingEnvironment I get the ApplicationBasePath. The problem is that the paths I get back are very different on localhost and on Azure. Azure: "D:\home\site\approot\src\src" Localhost: "C:\Users\deebo\Source\mysite\site\src" I want to use the base path to get the full path to a folder containing some images: wwwroot/img/gallery/ I got around this with the following code: var rootPath =

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

本小妞迷上赌 提交于 2019-12-05 00:23:06
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(ModelState); } _context.Country.Add(country); try { await _context.SaveChangesAsync(); } catch

MVC ICollection<IFormFile> ValidationState always set to Skipped

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 00:05:40
As part of an ASP.NET Core MVC 1.0 project, I have a ViewModel with an ICollection<> property. I need to validate that this collection contains one or more items. My custom validation attribute doesn't get executed. In my instance it holds multiple file attachments from a multipart/form-data form. I have decorated the property in the ViewModel with a custom validation attribute: [RequiredCollection] public ICollection<IFormFile> Attachments { get; set; } Below is the custom attribute class. It simply checks the collection is not null and has greater than zero elements: public class

ASP.NET Core 2.1 Custom RoleProvider with Windows Authentication

自闭症网瘾萝莉.ら 提交于 2019-12-04 23:58:05
问题 I am migrating applications away from the ASP.Net MVC 5 framework to the new .Net Core 2.1. I used Windows Authentication with a Custom RoleProvider in the MVC 5 Projects as shown in the link below. ASP.NET MVC How to create a custom role provider How do I accomplish the same in Core 2.1 as it does not seem to contain RoleProvider capability? Every example I come across uses Individual Accounts with IdentityUser and IdentityRole. My custom tables for User and Roles : public class User {

DateTime Data Annnotations do not work in ASP.NET Core RC2 application

烂漫一生 提交于 2019-12-04 23:52:52
问题 if I add Data annotations to my ViewModel the value of the DateTime field is always {1/1/0001 12:00:00 AM}. ViewModel public class EditReleaseViewModel : BaseViewModel { [Display(Name = "ReleaseDate", ResourceType = typeof(SharedResource))] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy HH:mm}")] [Required(ErrorMessageResourceName = "FieldRequired", ErrorMessageResourceType = typeof(SharedResource))] public DateTime ReleaseDate { get; set; } } The controller is

Logging to database in ASP.NET Core MVC 6

允我心安 提交于 2019-12-04 23:47:21
问题 In my old MVC5 project all log informations was saved on a sql server database through log4net. I would like keep logger structure of ASP.NET 5 without using any more the log4net. Is it possibile saving the logs on a database table? What kind of code should I use? 回答1: You can implement ILoggerFactory and ILogger however you like including, for example, logging to a database. Here's an example of an ILogger implementation that uses EntityFramework 6 to save logs to a database. 回答2: I've