asp.net-core-mvc

Custom authorization attribute in .NET Core [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-21 12:14:24
问题 This question already has answers here : How do you create a custom AuthorizeAttribute in ASP.NET Core? (8 answers) Closed 2 years ago . I'm building an API in .NET Core 1.1. I build a custom User object from HttpContext.User in a base controller that all of my other controllers inherit from, and I have authentication enabled by default (must be manually disabled with [AllowAnonymous] when necessary). The User object has an IsAdmin property. Right now I'm checking if the user is an admin at

HtmlHelpers in MVC 6

情到浓时终转凉″ 提交于 2019-12-21 10:58:26
问题 I'm trying to port this code over to mvc 6, any help is appreciated, the code compiles but the method is not available in my views on @Html.IsActive . using Microsoft.AspNet.Mvc.Rendering; namespace Blah.Web.Helpers { public static class HtmlHelpers { public static string IsActive(this HtmlHelper htmlHelper, string controller, string action) { var routeData = htmlHelper.ViewContext.RouteData; var routeAction = routeData.Values["action"].ToString(); var routeController = routeData.Values[

How to change PasswordValidator in MVC6 or AspNet Core or IdentityCore

丶灬走出姿态 提交于 2019-12-21 10:00:17
问题 In the Asp.Net MVC 5 using Identity, was possible to do the following: manager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireLowercase = true, RequireDigit = false, RequireUppercase = false }; How to change the same configuration in MVC 6? I see that can be in ConfigurationServices method in the segment: services.AddIdentity<ApplicationUser, IdentityRole>() .AddPasswordValidator<>() But I could not use. 回答1: The Solution Beta6 In the Startup.cs write the code:

Is it possible to serve static files from outside the wwwroot folder?

安稳与你 提交于 2019-12-21 07:45:53
问题 I have an ASP.NET MVC 6 project with the following structure: project/ wwwroot/ custom/ project.json I want to serve files from custom as it if was a virtual folder into http://localhost/custom without having to copy them during development. Is it possible to do this in vNext without a virtual folder from IIS (say, using the StaticFile middleware)? 回答1: You can set the file provider on the options object when using the middleware. app.UseStaticFiles(new StaticFileOptions() { FileProvider =

Access Configuration/Settings on static class - Asp Core

空扰寡人 提交于 2019-12-21 07:37:26
问题 I have 3 solutions. Project.Web, Project.Core (Business), and Project.Layer(Models). In Project.Core, I have a static file that I can call like this Business.GetAllData(); from Project.Web.Controller. This calls DAL/EF files and gets data( BusinessDal.GetData() ). using (DBContext db = new DBContext()) { return db.GetAllData().ToPOCO(); } On my configuration/DbContext.cs, I have this: protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { #if DEBUG optionsBuilder

Access Configuration/Settings on static class - Asp Core

南笙酒味 提交于 2019-12-21 07:36:14
问题 I have 3 solutions. Project.Web, Project.Core (Business), and Project.Layer(Models). In Project.Core, I have a static file that I can call like this Business.GetAllData(); from Project.Web.Controller. This calls DAL/EF files and gets data( BusinessDal.GetData() ). using (DBContext db = new DBContext()) { return db.GetAllData().ToPOCO(); } On my configuration/DbContext.cs, I have this: protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { #if DEBUG optionsBuilder

Change the JSON serialization settings of a single ASP.NET Core controller

强颜欢笑 提交于 2019-12-21 07:29:46
问题 I'm having two controller controllers: ControllerA and ControllerB . The base class of each controller is Controller . The ControllerA needs to return JSON in the default format (camelCase). The ControllerB needs to return data in a different JSON format: snake_case. How can I implement this in ASP.NET Core 2.1? I've tried the startup with: services .AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Converters.Add(new StringEnumConverter()); options.SerializerSettings

VS 2017 always times out debugging an MVC Core website on IIS Express profile

吃可爱长大的小学妹 提交于 2019-12-21 07:26:56
问题 On a plain vanilla, scaffolded ASP.NET Core MVC web app, with a DbContext registered in the DI container, whenever I hit F5 after 30-60 seconds, I get the error message: Unable to start program 'http://localhost:60175/'. Operation timed out. The home page in an index view, whose action gets all employees from a localhost SQL Server db. The project is already built, so the initial build for debug is quite quick, and doubtfully the cause of the delay. If I run without debugging, I get the home

How do you reference the executing assembly in DNX Core 5.0 (ASP.NET 5)?

隐身守侯 提交于 2019-12-21 07:06:28
问题 I am porting some code from .NET 3.5 - 4.5. Inside of my assembly, I have some code that reads the resource from the currently executing assembly. However, GetExecutingAssembly() isn't a method on the Assembly type in DNX core 5.0. var xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(xsdPath); What is the equivalent of Assembly.GetExecutingAssembly() in DNX core 5.0? Or if I need a namespace to get that method back (an extension method perhaps?), what is the namespace?

How do I dynamically choose a controller in MVC Core

亡梦爱人 提交于 2019-12-21 05:41:34
问题 I have a situation where a site may need a link to redirect to certain controllers based on database results. For example: site.com/abcd needs to return the result from a Item Controller, which would normally be called as /item/view/123 The key here is that I can't hard code the abcd into the routing. And some links may go to an Item Controller, others may go to an Orders controller. I've tried a catchall route to a controller than then loads up the desired controller, but the environment is