ASP.NET MVC 6 Tutorial :: Clean up Controller Class in ASP.NET MVC

ASP.NET is used by millions of developers worldwide to build web apps. With each release, Microsoft changes ASP.NET for the better and version 5 with ASP.NET MVC 6 is no exception. Microsoft says that this upgrade makes “ASP.NET leaner, more modular, cross-platform, and cloud optimized.”

hflnet52

In this article, I will tell you about How to Clean up Controller Class in ASP.NET MVC 6. Here may be a tiny piece and the way i use some easy tricks to assist clean up my Controllers in ASP.NET MVC.

The first issue within the code is easy declaration of an EntityContext – this can be enforced directly, however might (and sometimes should) be implemented differently for dependency injection, however you get the thought. Here is wherever I conjointly prefer to include stuff that’s typically used and will be very important (and centralized) for an application like an output sort for web services (JSON, XML, etc.).

Next the OnActionExecuting is over-ridden and also the Context is initialized. Here is wherever you’ll be able to initialize the properties you set above.

The next methodology, the HttpPost Index method is simply an example of however often times you’ll be able to consolidate a normally used method. For this instance, it absolutely was from an application that had searches on all index pages. Rather than repeating this code in each controller, you’ll be able to simply place it here.

The final methodology has become terribly helpful. A use I often realize, is when taking in a large model when an ASP.NET MVC POST event, I will attach the model to the db generically with none further work

This method may be a bit confusing, however it’s merely attaching the new model to the db while not a db lookup. In my tables, I typically have a field ‘Id’ that houses the primary Key of every table, whether or not it’s an Int or Guid. This way, I will merely pass in whatever object i’m currently working with, and by using the pluralize method, and reflection, the method will find out that table to connect the model to – eliminating the requirement on behalf of me to try and do extra writing. or else, if i need to change the Key from ‘Id’, I will pass that in as well.

Now once I get a model being posted, it’s simple to deal with:

This avoids the need to take another trip to the db, change the properties on the object, then submit – streamlining the method very much and lowering plenty of code. This would be an example of however this is able to be done manually with the normal Controller class:

Not a large difference once you are simply getting a pair of values, however you may see how a model with twenty or thirty fields are often block from 30+ lines to just 1 or 2.

HostForLIFE.eu revolutionized hosting with Plesk Control Panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools. Plesk completes requests in seconds. It is included free with each hosting account. Renowned for its comprehensive functionality – beyond other hosting control panels – and ease of use, Plesk Control Panel is available only to HostForLIFE’s customers. They offer a highly redundant, carrier-class architecture, designed around the needs of shared hosting customers.

Leave a Reply

Your email address will not be published. Required fields are marked *