European ASP.NET MVC 4 and MVC 5 Hosting

BLOG about ASP.NET MVC 3, ASP.NET MVC 4, and ASP.NET MVC 5 Hosting and Its Technology - Dedicated to European Windows Hosting Customer

ASP.NET MVC Hosting - HostForLIFE.eu :: Creating Layout (Master) Pages In MVC

clock September 26, 2025 09:42 by author Peter

This article explains how we can create the Layout (Master) page in MVC 5. Also, it explains the components of Master Page.

Creating Layout Pages


Create a new MVC Project.

Add MVC Layout page in Share folder.
Note: Share folder pages can be used across the project.

Add the below content to the Layout page.

Nav- Nav tags are generally used for navigation bar. We can add links to navigate between the pages.

@RenderSection- Render section is just like a place holder where we can place our code that we can inject from the content page. If we make render section property true, we must need to add this section in all the pages which are used in this layout page, otherwise it will go through as a not implemented error. If we want to make this an option, we need to set it to false.

@RenderBody- RenderBody is a place holder to display the content of the View.

Header & Footer- These are useful to display the header and footer information.

Add Controller and its View to create your page.

Select a new layout page while creating the View.

The below Index View is created by using _RKMaster.cshtml page. In general, if you use _Layout.cshtml, it doesn't display here because it is set in the global default Master page. If we want to set our Layout page as default layout page, we need to set it in _ViewStart.html page, as shown below.

Add the Render section and page content in the View.

Configure your Controller in Route Config to take it as default, and execute the project.

Final View- The header, nav, and footer are coming from the Master Layout page, while the section and page body are rendering from the View page.

Hope this article helps you in understanding the Layout pages and its content.



ASP.NET MVC Hosting - HostForLIFE.eu :: How To Use Cookie In ASP.NET Core Application?

clock September 12, 2025 08:02 by author Peter

Anyone interested in learning how to use the Asp.net core MVC application to save data in browser cookies should read this post. You can use the ASP.NET Core MVC web application to develop a sample web application if you wish to put it into practice.

Let's begin.

In essence, a cookie is a tiny bit of information sent to the user's web browser by the server. It might be saved by the browser and sent back to the same server with a subsequent request. Usually, the browser uses it to determine whether two requests originated from the same browser.

Types of cookie 
Persistent cookie

A cookie that has not to have expired time which is called a persistent cookie 

Non-Persistent cookie

Cookie which has expired time is called a Non-persistent cookie 

Adding cookie to the browser
First, add methods inside the Home controller. I have created an action method as CreateCookie and I have added key as DemoCookie. After that I have stored string value as Yogesh so we can store this value in the client browser 
public IActionResult CreateCookie() {
    string key = "DemoCookie:;
    string value = Yogesh;
    cookieOptions obj = new CookieOptions();
    obj.Expires = DateTime.Now.AddDays(7);
    Response.Cookie.Append(key, value, options);
    return view();
}


To see the cookie which is added in the browser I am using Google Chrome.

Now we will see how to retrieve data from cookies, here I have created Action Method ReadData that is used to retrieve data from cookies.
public IActionResult Read Data() {
    string key = "DemoCookie";
    var CookieValue = Request.Cookies[key];
    return View();
}

Now we will see how to delete data that is stored in cookies, here I have created the Action method Delete to perform the Delete operation on the cookie.

public IActionResult Delete() {
    string key = "DemoCookie";
    string value = DateTime.Now.ToString();
    CookieOptions options = new CookieOptions();
    options.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Append(key, value, options);
    return View();
}

Summary 
In this article, we have seen how to store data into cookies and retrieve and delete data from cookies. Hope you guys like it. Happy Coding!



ASP.NET MVC Hosting - HostForLIFE.eu :: A Comprehensive Guide on Partial View vs. ViewComponent in ASP.NET MVC/Core

clock September 3, 2025 07:12 by author Peter

Both appear to be comparable at first glance. They enable us to reuse user interface elements on other sites. However, they differ greatly under the hood. The distinctions, applications, performance factors, and when to pick one over the other will all be covered in this article.

What is a Partial View?

  • A Partial View is a Razor view (.cshtml) file that can be reused inside other views.
  • It is essentially a fragment of HTML + Razor that does not have its own controller logic.

Think of it like a shared template. If you’ve worked with ASP.NET WebForms before, you can compare it to a UserControl (.ascx).
Example

_LoginPartial.cshtml
@if (User.Identity.IsAuthenticated)
{
    <p>Welcome, @User.Identity.Name</p>
    <a href="/Account/Logout">Logout</a>
}
else
{
    <a href="/Account/Login">Login</a>
}

You can include it in your main view as:
@Html.Partial("_LoginPartial")

or asynchronously:

@await Html.PartialAsync("_LoginPartial")
Key Point: A Partial View does not have its own logic. It depends on the data passed from the parent view’s controller.

What is a ViewComponent?
A ViewComponent is like a mini-controller.
It combines C# logic + a Razor view into a self-contained unit.

It allows you to encapsulate business logic and rendering in one place, making it perfect for widgets that require their own data fetching.

Example
ViewComponents/CartSummaryViewComponent.cs
public class CartSummaryViewComponent : ViewComponent
{
    private readonly ICartService _cartService;

    public CartSummaryViewComponent(ICartService cartService)
    {
        _cartService = cartService;
    }

    public IViewComponentResult Invoke()
    {
        var cart = _cartService.GetCart(User.Identity.Name);
        return View(cart);
    }
}


The Razor view (Views/Shared/Components/CartSummary/Default.cshtml):
<div class="cart-summary">
    <p>Items in cart: @Model.Items.Count</p>
    <p>Total: @Model.TotalPrice.ToString("C")</p>
</div>

Usage in your main view:
@await Component.InvokeAsync("CartSummary")

Key Point: A ViewComponent has its own logic and data source. It does not rely on the parent controller’s model.

Key Differences Between Partial View and ViewComponent

FeaturePartial ViewViewComponent
Definition Reusable HTML + Razor fragment Mini controller + view
Logic Handling No (uses parent’s controller/model) Yes (has its own C# logic class)
Data Binding Uses parent ViewData, ViewBag, Model Accepts parameters, independent model
Execution Rendered inside parent’s lifecycle Executes independently like a child action
Reusability Best for static/common layouts Best for dynamic, data-driven widgets
Performance Lightweight Slightly heavier but cleaner separation
Examples Header, Footer, Menu, Static widgets Notifications, Cart summary, Latest posts

When to Use Partial View?

Use a Partial View when:

  • You only need reusable HTML markup.
  • Data comes from the parent controller.
  • Example scenarios:
  • Website header and footer
  • Navigation menus
  • Shared static templates like the Terms & Conditions section

Code Example

@Html.Partial("_Header")
@Html.Partial("_Footer")


When to Use ViewComponent?

Use a ViewComponent when:

  • You need business logic + view rendering together.
  • Data comes from a different source than the parent view.
  • You want testability (since ViewComponents can be unit tested).
  • Example scenarios:
    • Shopping cart summary
    • Latest blog posts widget
    • Notification panel
    • User profile box

Code Example
@await Component.InvokeAsync("LatestPosts", new { count = 5 })

Performance Considerations

  • Partial View is faster for simple static rendering because it does not invoke additional logic.
  • ViewComponent is slightly heavier, but it provides better separation of concerns and testability.
  • In real-world projects, mixing both approaches works best.

Conclusion
ASP.NET MVC/Core offers two strong tools for creating modular, maintainable applications: Partial Views and ViewComponents.

  • For basic, static UI fragments, use partial views.
  • For reusable, logic-driven user interface elements, use ViewComponents.

You may improve the scalability, maintainability, and cleanliness of your application by selecting the appropriate strategy for each situation.



About HostForLIFE.eu

HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes.

We have offered the latest Windows 2016 Hosting, ASP.NET Core 2.2.1 Hosting, ASP.NET MVC 6 Hosting and SQL 2017 Hosting.


Tag cloud

Sign in