SlideShare une entreprise Scribd logo
1  sur  11
ASP.NET
MVC Best
Practices
Presented by Tanveer Yousuf
Models
• Use a consistent naming convention for your model classes.
• Use a singular word for a model class.
• Ensure that your model has a parameter-less public constructor.
• Make full use of the annotation attribute classes to embed basic validation
and data integrity mechanism in your model classes. Examples are
RequiredAttribute, DataTypeAttribute, StringLengthAttribute, etc.
• Create custom validation attributes for custom validation and ensure that
you implement both server-side and client-side logic.
Models (Contd.)
• Make non-required numeric and Boolean properties as nullable.
• Use inheritance to reuse common properties across logically related model
classes. For example, if you have EmployeeModel and ContractorModel
classes then having them inheriting from a common model like
PersonModel makes good sense.
Models (Contd.)
• Don’t use complex types for your model properties.
• Don’t create methods in a model class. Models are supposed to be plain
classes with read-write properties.
• Don’t create static properties. If you do need few of then they probably
belong somewhere else or perhaps you may want to create few constants in
that case.
• Never make a reference to a controller class or a view within a model.
Model is not supposed to know anything about a controller or a view’s
existence.
Views
• Ensure the folder structure and the names of your views match with your
controllers and controller actions.
• Any block of HTML that is repeated in more than one view should go inside
a layout view or a partial view.
• Use ViewBag object to pass data to layout views.
• The HTML for displaying messages should be placed in a layout view or a
partial view.
• Layout and partial views should be prefixed with _ character.
Views (Contd.)
• Don’t import namespaces within a view. Instead make use of the
namespaces tag in the web.config file.
• Never make a reference to a controller class within a view with the
exception of generating action links.
• Never retrieve data from a data source or a business component directly
from a view. That’s the controller job!
Controllers
• Always have a Home controller.
• Always have an Index action for every controller.
• Use a consistent naming convention.
• Each controller should be responsible for a single logical domain object.
• Choose a plural word for a controller especially if the Index action of the
controller renders a list of data.
• Try to name controller actions with simple verbs as applicable. Examples are:
New, Edit, and Delete.
Controllers (Contd.)
• Use the Authorize attribute at the class level unless the controller by
definition allows anonymous access to all its actions. Applying Authorize
attribute at the class level and then selectively applying AllowAnonymous
attribute on the actions that need it is a good strategy.
• Apply output caching on controller actions that render static HTML or
content that doesn’t have to be real-time. You should apply output caching
at the controller level if applicable.
Controllers (Contd.)
• Don’t duplicate utility code in controllers such as displaying messages.
Instead create a common controller class (that inherits from the Controller
class) with common methods and properties.
• Don’t duplicate exception management logic in your controller actions.
Instead use the ExceptionFilterAttribute to handle exceptions.
Thank You
Ejyle Technologies Private Limited
2nd
Floor, Globe House, 105, Richmond Road, Bangalore-560025, India | Phone: +9141312223
Email: info@Ejyle.com | Website: http://www.ejyle.com | Blog: http://blog.ejyle.com
Facebook: http://www.facebook.com/ejyle | Twitter: @ejyle | LinkedIn: http://www.linkedin.com/company/ejyle

Contenu connexe

En vedette

Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406
Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406
Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406Hugo Vallet
 
Comunidad Ibérica Global La Revista Abr12
Comunidad Ibérica Global La Revista Abr12Comunidad Ibérica Global La Revista Abr12
Comunidad Ibérica Global La Revista Abr12directivos
 
La Inserción de la Informática en la Educacion
La Inserción de la Informática en la EducacionLa Inserción de la Informática en la Educacion
La Inserción de la Informática en la EducacionWalter Toledo
 
I decide fast lecture 8 resources
I decide fast lecture 8 resourcesI decide fast lecture 8 resources
I decide fast lecture 8 resourcesStanford University
 
Книга доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&Beauty
Книга доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&BeautyКнига доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&Beauty
Книга доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&BeautyJulia julia
 
OpenROV: Node.js takes a dive into the ocean
OpenROV: Node.js takes a dive into the oceanOpenROV: Node.js takes a dive into the ocean
OpenROV: Node.js takes a dive into the oceanSimone Chiaretta
 
SIIMA Awards FB Promo
SIIMA Awards FB PromoSIIMA Awards FB Promo
SIIMA Awards FB PromoRaj ch
 
01 acceso y asignacion de direccion ip estatica mikrotik
01 acceso y asignacion de direccion ip estatica mikrotik01 acceso y asignacion de direccion ip estatica mikrotik
01 acceso y asignacion de direccion ip estatica mikrotikbeppo
 
P3 PT1 Letter 2013
P3 PT1 Letter 2013P3 PT1 Letter 2013
P3 PT1 Letter 2013lizsaid
 
Estudio. Redes Sociales Corporativas en Latinoamérica
Estudio. Redes Sociales Corporativas en LatinoaméricaEstudio. Redes Sociales Corporativas en Latinoamérica
Estudio. Redes Sociales Corporativas en Latinoaméricalordjavier
 
ProgramacióN 4º Eso
ProgramacióN 4º EsoProgramacióN 4º Eso
ProgramacióN 4º EsoPilar Torres
 
New_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_Dalziel
New_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_DalzielNew_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_Dalziel
New_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_DalzielNurdilek Hacıalioğlu Dalziel
 
Noray Nomina - Actualización 16.08
Noray Nomina - Actualización 16.08Noray Nomina - Actualización 16.08
Noray Nomina - Actualización 16.08Noray
 

En vedette (16)

Art09
Art09Art09
Art09
 
Capitulo 27.
Capitulo 27.Capitulo 27.
Capitulo 27.
 
Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406
Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406
Valeo Braking Systems Brake Fluid DOT 4+ material safety data sheet 402406
 
Comunidad Ibérica Global La Revista Abr12
Comunidad Ibérica Global La Revista Abr12Comunidad Ibérica Global La Revista Abr12
Comunidad Ibérica Global La Revista Abr12
 
Walco Solutions
Walco SolutionsWalco Solutions
Walco Solutions
 
La Inserción de la Informática en la Educacion
La Inserción de la Informática en la EducacionLa Inserción de la Informática en la Educacion
La Inserción de la Informática en la Educacion
 
I decide fast lecture 8 resources
I decide fast lecture 8 resourcesI decide fast lecture 8 resources
I decide fast lecture 8 resources
 
Книга доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&Beauty
Книга доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&BeautyКнига доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&Beauty
Книга доктора Дельбе по гелям Алое Вера, Пробаланс, Колострум. LR Health&Beauty
 
OpenROV: Node.js takes a dive into the ocean
OpenROV: Node.js takes a dive into the oceanOpenROV: Node.js takes a dive into the ocean
OpenROV: Node.js takes a dive into the ocean
 
SIIMA Awards FB Promo
SIIMA Awards FB PromoSIIMA Awards FB Promo
SIIMA Awards FB Promo
 
01 acceso y asignacion de direccion ip estatica mikrotik
01 acceso y asignacion de direccion ip estatica mikrotik01 acceso y asignacion de direccion ip estatica mikrotik
01 acceso y asignacion de direccion ip estatica mikrotik
 
P3 PT1 Letter 2013
P3 PT1 Letter 2013P3 PT1 Letter 2013
P3 PT1 Letter 2013
 
Estudio. Redes Sociales Corporativas en Latinoamérica
Estudio. Redes Sociales Corporativas en LatinoaméricaEstudio. Redes Sociales Corporativas en Latinoamérica
Estudio. Redes Sociales Corporativas en Latinoamérica
 
ProgramacióN 4º Eso
ProgramacióN 4º EsoProgramacióN 4º Eso
ProgramacióN 4º Eso
 
New_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_Dalziel
New_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_DalzielNew_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_Dalziel
New_Media_Conf_Ist_Gelisim_Unv_May_2015_Nurdilek_Dalziel
 
Noray Nomina - Actualización 16.08
Noray Nomina - Actualización 16.08Noray Nomina - Actualización 16.08
Noray Nomina - Actualización 16.08
 

Dernier

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 

Dernier (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 

ASP.NET MVC Best Practices

  • 2. Models • Use a consistent naming convention for your model classes. • Use a singular word for a model class. • Ensure that your model has a parameter-less public constructor. • Make full use of the annotation attribute classes to embed basic validation and data integrity mechanism in your model classes. Examples are RequiredAttribute, DataTypeAttribute, StringLengthAttribute, etc. • Create custom validation attributes for custom validation and ensure that you implement both server-side and client-side logic.
  • 3. Models (Contd.) • Make non-required numeric and Boolean properties as nullable. • Use inheritance to reuse common properties across logically related model classes. For example, if you have EmployeeModel and ContractorModel classes then having them inheriting from a common model like PersonModel makes good sense.
  • 4. Models (Contd.) • Don’t use complex types for your model properties. • Don’t create methods in a model class. Models are supposed to be plain classes with read-write properties. • Don’t create static properties. If you do need few of then they probably belong somewhere else or perhaps you may want to create few constants in that case. • Never make a reference to a controller class or a view within a model. Model is not supposed to know anything about a controller or a view’s existence.
  • 5. Views • Ensure the folder structure and the names of your views match with your controllers and controller actions. • Any block of HTML that is repeated in more than one view should go inside a layout view or a partial view. • Use ViewBag object to pass data to layout views. • The HTML for displaying messages should be placed in a layout view or a partial view. • Layout and partial views should be prefixed with _ character.
  • 6. Views (Contd.) • Don’t import namespaces within a view. Instead make use of the namespaces tag in the web.config file. • Never make a reference to a controller class within a view with the exception of generating action links. • Never retrieve data from a data source or a business component directly from a view. That’s the controller job!
  • 7. Controllers • Always have a Home controller. • Always have an Index action for every controller. • Use a consistent naming convention. • Each controller should be responsible for a single logical domain object. • Choose a plural word for a controller especially if the Index action of the controller renders a list of data. • Try to name controller actions with simple verbs as applicable. Examples are: New, Edit, and Delete.
  • 8. Controllers (Contd.) • Use the Authorize attribute at the class level unless the controller by definition allows anonymous access to all its actions. Applying Authorize attribute at the class level and then selectively applying AllowAnonymous attribute on the actions that need it is a good strategy. • Apply output caching on controller actions that render static HTML or content that doesn’t have to be real-time. You should apply output caching at the controller level if applicable.
  • 9. Controllers (Contd.) • Don’t duplicate utility code in controllers such as displaying messages. Instead create a common controller class (that inherits from the Controller class) with common methods and properties. • Don’t duplicate exception management logic in your controller actions. Instead use the ExceptionFilterAttribute to handle exceptions.
  • 11. Ejyle Technologies Private Limited 2nd Floor, Globe House, 105, Richmond Road, Bangalore-560025, India | Phone: +9141312223 Email: info@Ejyle.com | Website: http://www.ejyle.com | Blog: http://blog.ejyle.com Facebook: http://www.facebook.com/ejyle | Twitter: @ejyle | LinkedIn: http://www.linkedin.com/company/ejyle