asp.net-core-mvc

Accessing environment variables from ASP.NET Core 1 RC1 IConfiguration

杀马特。学长 韩版系。学妹 提交于 2019-12-11 12:37:39
问题 I am working on learning how to properly handle environment variables and configuring my app in multiple environments. I've chosen to read from a config.json in Development, and use environment variables in Production. I have the following Startup.cs to demonstrate this: using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Extensions.Configuration; namespace Variables { public class Startup { private IConfiguration mConfiguration; public

ASP.NET 5 MVC 6 Configuration in Class

最后都变了- 提交于 2019-12-11 12:36:32
问题 How can the configurations in appsettings.json be accessed from a non-static class that is not a controller and can't receive IOptions in the constructor? 回答1: Can you use property or method injection? And are you allowed to change the default container? If so: First change your default container to for instance Autofac (reference Autofac.Extensions.DependencyInjection version 4.0.0-rc1-177 in your project.json). Change your ConfigureServices as follows: public IServiceProvider

ViewModel DisplayFormat Data Annotation not rendered in View

瘦欲@ 提交于 2019-12-11 11:52:43
问题 Using ASP.NET 5 Web Application Template I am trying to render a date in short date format. My viewmodel looks like this [Display(Name = "Subscription Expires")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] public DateTime? SubscriptionExpires { get; set; } My view looks like this <dt><label asp-for="SubscriptionExpires"></label>:</dt> <dd> @Model.SubscriptionExpires </dd> However @Model.SubscriptionExpires renders in long date format. How do I get the view to

Razor & nested loops

谁都会走 提交于 2019-12-11 10:44:34
问题 I am just started to learn Razor and have a question regarding nested loops. It renders everything correctly using the following code @foreach (var group in Model.Groups) { foreach (var item in group.Items) { <span>@item.Title</item> } } but does not when I wrap the second foreach with "div" tags. It saying the item variable does not exist in that case. @foreach (var group in Model.Groups) { <div> foreach (var item in group.Items) { <span>@item.Title</item> } </div> } I made it work using the

DbContext cannot find database

一曲冷凌霜 提交于 2019-12-11 10:42:05
问题 The connection string for our app is set in appsettings.json "Data": { "DefaultConnection": { "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=Customers;Trusted_Connection=True;MultipleActiveResultSets=true", In ConfigureServices we have services.AddEntityFramework() .AddSqlServer() .AddDbContext<CustomersContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); This seems to work in cases like this var membershipUser = await _userManager

TagHelper validation when editing a list

我只是一个虾纸丫 提交于 2019-12-11 10:18:21
问题 I am trying to use TagHelpers when editing a list of items. This is the code inside the view where helpers are employed: @for (var i = 0; i < Model.Items.Count; i++) { <tr> <td> <input asp-for="Items[i].Name" /> <span asp-validation-for="Items[i].Name" class="text-danger" /> </td> </tr> } Input helpers are working as expected, ModelState is properly filled with validation errors but validation errors are not shown to the user. I'm guessing there's a problem with rendering validation tags. <td

How to setup Entity Framework in ASP.NET Core project

老子叫甜甜 提交于 2019-12-11 10:10:24
问题 I have a new ASP.NET Core project where I am trying to simply query a database and display results of the query on a page. I'm new to using ASP.NET Core and have found from researching that it would be best to use Entity Framework in order to query the database. I was trying to follow this guide in order to figure out how to connect to the database via the Entity Framework, but there is a step in the guide that references a file called project.json to which the line: "Microsoft

Application Menu with Security Trimming

夙愿已清 提交于 2019-12-11 09:19:59
问题 I came from asp.net 2.0 webforms; where i just define my menu in Web.sitemap with all the trimming taken care off. Is there any equivalent feature in asp.net-core-mvc for this seemingly easy task ? 回答1: You can create a custom TagHelper for it, inside this tag helper you can check whether user is in apporperiate role or not: public class SecurityTrimmingTagHelper : TagHelper { [ViewContext] public ViewContext Context { get; set; } public override void Process(TagHelperContext context,

How to make DateTime bind to RazorPages ViewModel?

孤街醉人 提交于 2019-12-11 08:33:21
问题 I have an ASP Core 2.2 project that uses RazorPages, I am binding some data, some of which I send to the client, the client needs to edit this data where required before sending it back. This works for everything perfectly fine until I want to bind a DateTime object. The data is bound to the HTML datetime-local control as expected, and according to the network traffic, it is sent back in the POST requests form data, however the DateTime object in the OnPost always reads as 01/01/0001 I have

Returning BadRequest in ASP.Net Core MVC to Microsoft jQuery Unobtrusive Ajax post has ModelState undefined

可紊 提交于 2019-12-11 08:26:24
问题 THE PROBLEM I am trying to return service side validation errors from ModelState using BadRequest(ModelState) but when I try to assign the the jQuery ModelState from xhr.responseText I am getting undefined error in javascript. I am using ASP.Net Core 1.0.1 with Microsoft.jQuery.Unobtrusive.Ajax 3.2.3. THE CODE Controller Action public async Task<IActionResult> ApplySanction(SanctionViewModel model) { try { if (ModelState.IsValid) { //update database return RedirectToAction("Details"); } else