DEV Community

Cover image for AddMVC() vs AddControllersWithViews() vs AddControllers() vs AddRazorPages() | ASP.Net Core
Ravi Vishwakarma
Ravi Vishwakarma

Posted on

AddMVC() vs AddControllersWithViews() vs AddControllers() vs AddRazorPages() | ASP.Net Core

In ASP.NET Core, the methods AddMvc(), AddControllersWithViews(), AddControllers(), and AddRazorPages() are used to configure services for various application architectures. Each of these methods serves a specific purpose depending on the type of application you are building.


AddMvc()

Purpose: Configures the app to use the complete ASP.NET Core MVC framework, including support for controllers, views, Razor Pages, and API features.

Status: This method is obsolete in recent ASP.NET Core versions (3.0 and later). It's replaced by more granular methods like AddControllersWithViews() or AddRazorPages().

Use Case: Used in older versions (before ASP.NET Core 3.0) to set up both MVC and Razor Pages in a single step.


AddControllersWithViews()

Purpose: Configures the app to support controllers and views (for MVC-style web apps). Does not include Razor Pages.

Use Case: Ideal for traditional MVC web applications where controllers and views are the primary focus.
Suitable for server-rendered HTML with routing, model binding, and validation.
Features: Adds support for controllers and views.
Does not support Razor Pages (e.g., .cshtml files under the /Pages folder).

Example:

builder.Services.AddControllersWithViews();
Enter fullscreen mode Exit fullscreen mode

AddControllers()

Purpose: Configures the app to support controllers only, for building API applications without views.

Use Case: Suitable for building Web APIs. Does not include Razor Pages or support for views.
Features: Adds support for controllers, but excludes Razor Pages and views. Focused on API endpoints, ideal for RESTful services.

Example:

builder.Services.AddControllers();
Enter fullscreen mode Exit fullscreen mode

AddRazorPages()

Purpose: Configures the app to support Razor Pages, a page-focused programming model.

Use Case: Ideal for applications primarily using Razor Pages for server-side rendering. Suitable for smaller apps or apps with a page-oriented architecture (e.g., intranet applications).

Features:
Supports Razor Pages (e.g., .cshtml files under the /Pages folder).
Does not support controllers or views unless explicitly added.

Example:

builder.Services.AddRazorPages();
Enter fullscreen mode Exit fullscreen mode

.NET Core Middleware Comparison Table

Method Controllers Views Razor Pages APIs (JSON/XML responses)
AddMvc()
AddControllersWithViews()
AddControllers()
AddRazorPages()

Choosing the Right Method

For APIs:

  • Use AddControllers().
  • Example: Building a RESTful API that does not require views or Razor Pages.

For Traditional MVC Applications:

  • Use AddControllersWithViews().
  • Example: Building a web app with controllers and views for server-side rendering.

For Razor Pages Applications:

  • Use AddRazorPages().
  • Example: Building a simple page-oriented application with .cshtml files under /Pages.

For Hybrid Scenarios:

  • Combine AddControllersWithViews() and AddRazorPages() for apps that need both controllers and Razor Pages.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Use specific configurations (e.g., AddControllersWithViews(), AddRazorPages()) instead of AddMvc() for better clarity and performance.
  • Choose the appropriate method based on your application's architecture and the required features.
  • If your app includes API endpoints and views, combine AddControllersWithViews() with features like JsonOptions to configure API behavior.

Connect with Me!
Thanks for reading! If you found this helpful, consider following me for more content like this.

🌟 LinkedIn
💻 GitHub

Your support motivates me to keep sharing valuable insights. Stay tuned for more! 🚀

Thanks for Reading.

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay