asp.net-core-mvc

mvc6 custom model binder not firing

前提是你 提交于 2019-12-12 15:12:10
问题 I'm trying to figure out model binding in current mvc6 (visual studio 2015 release candidate). This is what my code looks like so far: public class MyObjectModelBinder : IModelBinder { public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext.ModelType == typeof(MyObject)) { var model = new MyObject(); return Task.FromResult(new ModelBindingResult(model, bindingContext.ModelName, true)); } return Task.FromResult<ModelBindingResult>(null); } } The

Register filter for an area

落花浮王杯 提交于 2019-12-12 13:13:49
问题 Is there a way to register filter for specific area? For example below code register Authorization Filter globally(force to authenticate): services.AddMvc(config => { var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); config.Filters.Add(new AuthorizeFilter(policy)); }); I want this but only for some areas. 回答1: You could write a custom IFilterProvider which can create the filter dynamically. Example: public class CustomFilterProvider : IFilterProvider { public

Method not found: 'Void EntityFrameworkCore.Design.Internal.DbContextOperations

我们两清 提交于 2019-12-12 12:45:27
问题 I am trying to scaffold and getting following error: There was an error running the selected code generator Method not found: 'Void EntityFrameworkCore.Design.Internal.DbContextOperations 回答1: https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db?view=aspnetcore-2.0#install-entity-framework I suspect you are missing the assemblies that are necessary for scaffolding? I also assume you are trying to reverse engineer an existing db? example Scaffold-DbContext "Server=

Making MVC 6 deploy on IIS on Windows 10

▼魔方 西西 提交于 2019-12-12 12:29:54
问题 I'm using 1.0.0-rc1-update1 and on IIS Express it works fine, but when I deploy to IIS, I get a blank page. Nothing. No errors, nothing in the event log - this is the same problem even for a vanilla MVC6 web application, so I doubt it's my code! I am pointing the application to the www folder under the published folder and I have my app pool set up for .net 4 Am quite used to getting errors out of IIS, but this is a new one on me! Would anyone be able to help? Or maybe at least point me at

ASP.NET and EF Core - ModelState.IsValid always returns true for models generated by Scaffold-DbContext

偶尔善良 提交于 2019-12-12 12:25:01
问题 I've been unable to validate models generated by the Scaffold-DbContext command in my ASP.NET Core controllers. The required/max length property configurations are all in the onModelCreating method of the context class that EF core generated. protected override void OnModelCreating( ModelBuilder modelBuilder ) { modelBuilder.Entity<ModelClass>( entity => { entity.ToTable( "ModelClass", "schema" ); entity.Property( e => e.ModelClassCode ) .IsRequired() .HasMaxLength( 30 ); My controller

MaxLength annotation to maxlength input property

瘦欲@ 提交于 2019-12-12 12:18:19
问题 Using the following DataAnnotations in ASP.NET Core 1.1. It would be good to have the max length of the input in my MVC View to be set to restrict the users input. Model [Display(Name = "Post Code")] [MaxLength(8, ErrorMessage = "Maximum number of characters that can be entered is 8!")] public string PostCode { get; set; } View <label asp-for="PostCode"></label> <input style="font-weight: normal;" class="form-control" placeholder="Post Code" asp-for="PostCode" data-val="true" autofocus />

Publishing appsettings.json to different environment

只谈情不闲聊 提交于 2019-12-12 12:16:27
问题 I have three files appsettings.json which is a file that I want to store "general shared settings", then I have appsettings.Development.json and appsettings.Production.json . When I do a publish from Visual Studio, it seems like only appsettings.json is copied and not adding / merge or even just send a simple copy the deployment folder, to me this task should be incorporated into the deployment pipeline. The question is: how can I do it? What am I missing? Is it not supposed that these

Multiple Posted Types asp.net 5 MVC 6 API

孤人 提交于 2019-12-12 11:46:58
问题 I can use [FromBody] for single type , but is there any way to use multiple? From the searching and reading I've done there is not a way, but i don't know if a way has been added in MVC 6. If not, where would be best to start with custom way. What should i be researching and looking for best method or place to hook in just before model binding so i can include my own method? 回答1: The best way is to create a composite wrapper: public class Wrapper { public ModelA A { get; set; } public ModelB

How to correctly set exparation datetime for cookies on ASP.NET Core MVC

早过忘川 提交于 2019-12-12 11:40:22
问题 I'm trying to set a Cookie to the browser from back-end (Asp.Net core) which should expire on the next day same time minus 5 minutes. Here is the C# code from controller HttpContext.Response.Cookies.Append("MyCookie", "test cookie value", new Microsoft.AspNetCore.Http.CookieOptions { Expires = DateTimeOffset.UtcNow.AddDays(1).AddMinutes(-5) }); But to the browser it is coming with wrong expiration Datetime. For example if cookie expiration date was set to 2016-09-28 19:15, on the browser it

Using WebClient in ASP.NET 5

∥☆過路亽.° 提交于 2019-12-12 11:09:36
问题 I am working in the VS15 beta and trying to use WebClient. While System.Net is referenced, and the intellisense suggests the WebClient class is available, on build I get the following error: The type or namespace name 'WebClient' does not exist in the namespace 'System.Net' (are you missing an assembly reference?) MyProj.ASP.NET Core 5.0 HomeController.cs I am doing the following simplistic code: var client = new System.Net. var html = client.DownloadString(url); When I go to the definition