asp.net-mvc-controller

Cannot add controller in ASP.NET MVC project

与世无争的帅哥 提交于 2019-12-06 14:19:22
I'm trying to add a controller in my ASP.NET MVC project. I am using Entity Framework Code First (5.0), and when I right-click the Controllers folder in my project and selecting Add Controller : After selecting MVC controller with read/write actions and views, using Entity Framework and other settings, I get this message: There was an error generating '{0}'. Try rebuilding your project. Update Seems that the special constructor I added to the DbContext caused the problem. After commenting out the constructor, it's working back again, however this message appeared: Anyway I checked the activity

Inheritance of Authorized Roles in controller classes

给你一囗甜甜゛ 提交于 2019-12-06 06:46:55
I've created controller classes to assist with Role authorization. I have a base class ControllersAuthorities , which is the highest level of authority. I have created the other classes to extend each base class. [Authorize(Roles = "Owner")] public abstract class ControllerAuthorities:Controller { } [Authorize(Roles = "Admin")] public abstract class AdminController:ControllerAuthorities { } [Authorize(Roles = "Employee")] public abstract class EmployeeController:AdminController { } [Authorize(Roles = "Sales")] public abstract class SalesController:EmployeeController { } First question, will

How to get the database context in a controller

蓝咒 提交于 2019-12-05 22:04:55
问题 I am trying all day to figure out to get the ApplicationDbContext in the ManageController.cs of a default MVC 6 project. I went online and Googled a lot but no one seems to have the same problem as I have with it. It is probably simple but I can't figure it out. Anyone has an idea? Here is what I tried: IServiceProvider service = new IServiceProvider(); var _context = service.GetService<ApplicationDbContext>(); 回答1: Use constructor injection: public class ManageController { private readonly

keeping asp.net mvc controller size down

£可爱£侵袭症+ 提交于 2019-12-04 07:32:31
I have a Controller. "OrderController". Currently it's 1800 lines. I like to reduce the size. I'm using static helper methods which is fine but i'm using ninject to call my repositories so don't have access to the repositories in the static methods without passing the properties in. What are some good approaches for reducing controller noise? How to get a Thin Controller Refactor reusable functionalities that can apply to multiple types of output to ActionFilters . Consequence: Less repetitive code, thinner Controller actions, quicker future development Refactor reusable functionalities that

How to get the database context in a controller

情到浓时终转凉″ 提交于 2019-12-04 03:26:05
I am trying all day to figure out to get the ApplicationDbContext in the ManageController.cs of a default MVC 6 project. I went online and Googled a lot but no one seems to have the same problem as I have with it. It is probably simple but I can't figure it out. Anyone has an idea? Here is what I tried: IServiceProvider service = new IServiceProvider(); var _context = service.GetService<ApplicationDbContext>(); Use constructor injection: public class ManageController { private readonly ApplicationDbContext _context; public ManageController(ApplicationDbContext context) { _context = context; }

Keeping a controller thin (too many action methods)

廉价感情. 提交于 2019-12-03 06:49:56
问题 I'm working on my first real ASP.NET MVC project and I've noticed that the controller I've been working in is getting rather large. This seemingly goes against the best practice of keeping your controllers thin. I've done a good job keeping the business logic out of the controllers. I use a separate layer for that. Each action primarily calls a method in the business layer and coordinates the end result based on whether or not the modelstate is valid. That said, the controller has a large

Keeping a controller thin (too many action methods)

情到浓时终转凉″ 提交于 2019-12-02 20:28:44
I'm working on my first real ASP.NET MVC project and I've noticed that the controller I've been working in is getting rather large. This seemingly goes against the best practice of keeping your controllers thin. I've done a good job keeping the business logic out of the controllers. I use a separate layer for that. Each action primarily calls a method in the business layer and coordinates the end result based on whether or not the modelstate is valid. That said, the controller has a large number of action methods. Intuitively, I would like to break the controller down into sub-controllers but

How to inject User Manager to Account Controller with default Identity Model in Identity 2 using Ninject

末鹿安然 提交于 2019-12-02 05:54:00
问题 I use Ninject on a MVC 5 project with Identity 2. For rest of data context and controllers using that I have no problems with dependency injection. For Account controller that uses Identity 2 model I'm getting null UserManager when I try to login: public class AccountController : Controller { private ApplicationUserManager _userManager; public AccountController() { } public AccountController(ApplicationUserManager userManager) { UserManager = userManager; } public ApplicationUserManager

ASP.NET MVC Controller Naming Pluralization

自作多情 提交于 2019-11-29 21:11:05
RESTful conventions indicate using plural nouns over singular objects. What is the pluralization convention for naming ASP.NET MVC controllers, i.e. ProductController or ProductsController ? Cybermaxs Some MVC Frameworks use plurals, however the MVC project templates contains a controller called AccountController thus suggesting singlular naming. It doesn't matter. As with most things in the Asp.net MVC framework the choice is yours. There is no real conventions. It's my personal opinion but what matters is that you pick a scheme and be consistent! I'm going to have to disagree with the

Setting an alternate controller folder location in ASP.NET MVC

我与影子孤独终老i 提交于 2019-11-29 07:35:42
问题 We can an MVC app that uses the default folder conventions for the HTML views, but we'd like to set up alternate "Services" folder with controllers used only for web services returning xml or json. So the route "/Services/Tasks/List" would be routed to "/Services/TaskService.cs", while "/Tasks/List" would be routed to the standard "/Controllers/TaskController.cs" We'd like to keep the service controllers separate from the view controllers. We don't think areas or using another project will