The ASP.NET MVC is a discontinued web application framework developed by Microsoft, which implements the model–view–controller (MVC) pattern. ASP.Net Core and ASP.Net Core MVC are significant releases that introduce a more optimized developer experience, a better performing run time, and even cross-platform support that will allow you to run your applications on Windows, Mac and Linux. Follow along to check your knowledge around latest ASP.NET MVC interview question and answers for your next .NET Developer interview.
ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework.
Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including:
Layout pages are similar to master pages in traditional web forms. This is used to set the common look across multiple pages. In each child page we can find
@{
Layout = “~/Views/Shared/TestLayout1.cshtml”;
}This indicates child page uses TestLayout page as it’s master page.
Section are the part of HTML which is to be rendered in layout page. In Layout page we will use the below syntax for rendering the HTML –
@RenderSection(“TestSection”)And in child pages we are defining these sections as shown below –
@section TestSection{
<h1>Test Content</h1>
}More new features introduced in ASP.NET Web API framework v2.0 are as follows:
IHttpActionResultActions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type — ActionResult and it will be invoked from method InvokeAction() called by controller.
Scaffold templates are used to generate code for basic CRUD operations within your ASP.NET MVC applications against your database with the help Entity Framework.
Microsoft provides a helpful scaffolding engine, powered by T4 templates, that automates creating basic CRUD controllers and views for models in ASP.NET MVC applications that use the Entity Framework. (There are also WebAPI and MVC without Entity Framework scaffolders currently available.)
NonAction methods in MVC?In MVC all public methods have been treated as Actions. So if you are creating a method and if you do not want to use it as an action method then the method has to be decorated with NonAction attribute as shown below:
[NonAction]
public void TestMethod()
{
// Method logic
}Using ASP.NET Web API has a number of advantages, but core of the advantages are:
GET, POST, PUT, DELETE, etc. for all CRUD operationsMediaTypeFormatterASP.NET Core is a brand new cross-platform web framework built with .NET Core framework. It is not an update to existing ASP.NET framework. It is a complete rewrite of the ASP.NET framework. It works with both .NET Core and .NET Framework.
Main characterestics of ASP.NET Core:
Razor is the first major update to render HTML in MVC 3. Razor was designed specifically for view engine syntax. Main focus of this would be to simplify and code-focused templating for HTML generation. Below is the sample of using Razor:
@model MvcMusicStore.Models.Customer
@{ViewBag.Title = “Get Customers”;}
<div class=”cust”> <h3><em>@Model.CustomerName</em> </h3>ViewModel in MVC?ViewModel is a plain class with properties, which is used to bind it to strongly typed view. ViewModel can have the validation rules defined for its properties using data annotations.
Routing is a pattern matching mechanism of incoming requests to the URL patterns which are registered in route table. Class — “UrlRoutingModule” is used for the same process.
Yes, we can share a view across multiple controllers. We can put the view in the Shared folder. When we create a new MVC Project we can see the Layout page will be added in the shared folder, which is because it is used by multiple child pages.
RenderBody and RenderPage in MVC?RenderBody is like ContentPlaceHolder in web forms. This will exist in layout page and it will render the child pages/views. Layout page will have only one RenderBody() method.
RenderPage also exists in Layout page and multiple RenderPage() can be there in Layout page.
Below are the processed followed in the sequence:
Usually, WebAPI used for data services where MVC can generate more types of outputs (like template HTML views).
HTML Helpers are like controls in traditional web forms. But HTML helpers are more lightweight compared to web controls as it does not hold viewstate and events.
HTML Helpers returns the HTML string which can be directly rendered to HTML page. Custom HTML Helpers also can be created by overriding HtmlHelper class.
This is introduced in MVC5. In this type of routing, attributes are being used to define the routes. This type of routing gives more control over classic URI Routing. Attribute Routing can be defined at controller level or at Action level like:
[Route('{action = TestCategoryList}')] // Controller Level
[Route('customers/{TestCategoryId:int:min(10)}')] // Action LevelPartialView in MVC?PartialView is similar to UserControls in traditional web forms. Partial views are used for re-usability purpose . Since it's been shared with multiple views these are kept in shared folder. Partial Views can be rendered in following ways:
Html.Partial()Html.RenderPartial()ViewBag and ViewData in MVC?ViewBag is a wrapper around ViewData, which allows to create dynamic properties.
Advantage of viewbag over viewdata will be:
ViewBag no need to typecast the objects as in ViewData.ViewBag will take advantage of dynamic keyword which is introduced in version 4.0. But before using ViewBag we have to keep in mind that ViewBag is slower than ViewData.ViewResult() and ActionResult() in ASP.NET MVC?Consider:
public ViewResult Index()
{
return View();
}
public ActionResult Index()
{
return View();
}JsonResult and PartialViewResult.The only difference is that with the ActionResult one, your controller isn't promising to return a view - you could change the method body to conditionally return a RedirectResult or something else without changing the method definition.
And some ActionResult subtypes are:
ViewData and TempData?Html.Partial in MVC?Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction?Rust has been Stack Overflow’s most loved language for four years in a row and emerged as a compelling language choice for both backend and system developers, offering a unique combination of memory safety, performance, concurrency without Data races...
Clean Architecture provides a clear and modular structure for building software systems, separating business rules from implementation details. It promotes maintainability by allowing for easier updates and changes to specific components without affe...
Azure Service Bus is a crucial component for Azure cloud developers as it provides reliable and scalable messaging capabilities. It enables decoupled communication between different components of a distributed system, promoting flexibility and resili...