asp.net-core-mvc

Get Multiple Connection Strings in appsettings.json without EF

旧巷老猫 提交于 2019-12-01 02:12:41
问题 Just starting playing with the .Net Core RC2 by migrating a current MVC .Net app I developed. It looks like to me because of the way that configuration is handled with appsettings.json that if I have multiple connection strings I either have to use EF to retrieve a connectionstring or I have to create separate classes named for each connection string. All the examples I see either use EF (which doesn't make sense for me since I will be using Dapper) or the example builds a class named after

ASP.Net 5 MVC 6 - how to return invalid JSON message on POST using FromBody?

一曲冷凌霜 提交于 2019-12-01 02:02:15
问题 I am creating an ASP.Net 5 application with MVC 6, using .Net 4.5.1. I have a POST method that uses a FromBody parameter to get the object automatically. [HttpPost] public IActionResult Insert([FromBody]Agent agent) { try { var id = service.Insert(agent); return Ok(id); } catch (Exception ex) { return HttpBadRequest(ex); } } This is just a proof a concept, I won't return only the id on success or the full exception on error. When a valid JSON is sent everything works fine. However when an

ASP.NET Core NullReferenceException when just accessing model

不羁岁月 提交于 2019-12-01 01:37:16
问题 I am having trouble with attempting to create a view with a strongly typed model. No matter what I pass in as the model to a View() , I always receive a NullReferenceException when even just accessing the Model . I can't even check if the model is null before executing the rest of the razor page; simply doing a if (Model != null) also throws the same NullReferenceException . Index.cshtml @page @model EncodeModel @{ Layout = "~/Pages/Shared/_Layout.cshtml"; } <h2>Encode</h2> <div id="progress"

How can I retrieve AppSettings configuration back in Asp.Net MVC 6?

泄露秘密 提交于 2019-12-01 01:34:18
问题 Assuming that I am using the new DepencyInjection framework to configure my classes and dependencies in the new ASP.Net/vNext. How can I use, How can I get my pre-defined configuration settings? public void ConfigureServices(IServiceCollection services) { // Add Application settings to the services container. services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings")); // Add EF services to the services container. services.AddEntityFramework() .AddSqlServer() .AddDbContext

TagHelpers add custom class for LabelTagHelper based on validation attribute [Required]

三世轮回 提交于 2019-12-01 01:29:14
In Core MVC there is anew concept as Tag helpers. We could previously create custom html helpers to attach some classes based on the validation data annotations such as [Required]. As TagHelpers arq quite new area I cannot find anough resources to achieve the following: here is the view model: [Required] public Gender Gender { get; set; } view: <label class="control-label col-md-3 required" asp-for="Gender"></label> css: .required:after { content: "*"; font-weight: bold; color: red; } output: But I don't want to manully add the required css class in the label. Somehow I shoudl be able to

IdentityServer4: Add Custom default Claim to Client Principal for Client_Credential Granttype

女生的网名这么多〃 提交于 2019-12-01 00:51:26
问题 I am using IdentityServer4 and I am trying to add a custom default claim to my CLIENT when the token is created. This is possible if i use the implicit flow and IProfileService like shown below. public class MyProfileService : IProfileService { public MyProfileService() { } public Task GetProfileDataAsync(ProfileDataRequestContext context) { var claims = new List<Claim> { new Claim("DemoClaimType", "DemoClaimValue") }; context.IssuedClaims = claims; return Task.FromResult(0); } public Task

How to provide localized validation messages for validation attributes

社会主义新天地 提交于 2019-12-01 00:46:02
I am working on an ASP.NET Core application and I would like to override the default validation error messages for data-annotations, like Required , MinLength , MaxLength etc. I read the documentation at: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization and it seems that it does not cover what I was looking for... For instance, a validation error message for the Required attribute can always be the same for any model property. The default text just states: The {0} field is required , whereby the {0} placeholder will be filled up with the property´s display name. In my

How to Determine the ASP.NET Core Environment in my Gulpfile.js

匆匆过客 提交于 2019-11-30 23:05:22
问题 I am using ASP.NET Core MVC 6 using Visual Studio 2015. In my gulpfile.js script I want to know if the hosting environment is Development, Staging or Production so that I can add or remove source maps (.map files) and do other things. Is this possible? UPDATE Relevant issue on GitHub. 回答1: You can use the ASPNETCORE_ENVIRONMENT (Was formerly ASPNET_ENV in RC1) environment variable to get the environment. This can be done in your gulpfile using process.env.ASPNETCORE_ENVIRONMENT . If the

How to redirect unauthorized users with ASP.NET MVC 6

£可爱£侵袭症+ 提交于 2019-11-30 22:48:00
I want to know how to redirect users. I have a Controller Index() and I want only users with the role "Student" can enter there! So I use [Authorize(Roles="Student")] I wonder how can I redirect users who do not have this role to the homepage MVC5 (and older): You can do this by changing the loginUrl attribute on your web.config . Change it to the desired route: <authentication mode="Forms"> <forms loginUrl="~/Home/Index" timeout="2880" /> </authentication> MVC6: In MVC6 you can try this (inside the Startup.cs ): public void ConfigureServices(IServiceCollection services) { services.Configure

Razor page can't see referenced class library at run time in ASP.NET Core RC2

笑着哭i 提交于 2019-11-30 22:46:47
问题 I started a new MVC Web Application project for the RC2 release and I'm trying to add a class library as a project reference. I added a simple class library to my project and referenced it and got the following in the project.json file: "frameworks": { "net452": { "dependencies": { "MyClassLibrary": { "target": "project" } } } }, I can use this library in any of the Controllers and the Startup.cs files without any trouble but I get the following error at run time when I try and use the