Resolved An unhandled exception occurred while processing the request.

Joined
Aug 14, 2021
Messages
18
Programming Experience
Beginner
Hello.Did exactly like the tutorial said and still it doesnt work,checked the spelling,still doesnt work.Any suggestions? Thank you very much.If somebody finds the solution,you're doing God's work.

Index.cshtml code:​

C#:
@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="ASP.NET documentation">building Web apps with ASP.NET Core</a>.</p>
</div>
@section Summary {
    <div class="bg-info text-white m-2 p-2">
        @await Component.InvokeAsync("ProductSummary")

       
    </div>
}

_Layout.cshtml code:​

C#:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - ViewComponentsdemo</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ViewComponentsdemo</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    @if (IsSectionDefined("Summary"))
    {
        @RenderSection("Summary", false)
    }



    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2021 - ViewComponentsdemo - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
     @RenderSectionAsync("Scripts", required: false)
</body>
</html>

Productsdata code:​

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ViewComponentsdemo.Models
{
    public class Productsdata
    {
        private List<Product> _Products = new List<Product>
        {
            new Product {
                ProductID = 1,
                ProductName = "AMD Ryzen 3990",
                Quantity=100,
                UnitsInStock=50,
                Discontinued = false,
                Cost=3000,
                LaunchDate=new DateTime(2019,10,1)
            },
            new Product {
                ProductID = 2,
                ProductName = "AMD Ryzen 3970",
                Quantity=100,
                UnitsInStock=70,
                Discontinued = false,
                Cost=2500,
                LaunchDate=new DateTime(2019,10,5)
            },
               new Product {
                ProductID = 3,
                ProductName = "AMD Ryzen 3960",
                Quantity=100,
                UnitsInStock=80,
                Discontinued = false,
                Cost=2000,
                LaunchDate=new DateTime(2019,10,15)
            },
                  new Product {
                ProductID = 4,
                ProductName = "AMD Ryzen 3950",
                Quantity=100,
                UnitsInStock=40,
                Discontinued = false,
                Cost=1500,
                LaunchDate=new DateTime(2019,10,25)
            }
         };
        public IEnumerable<Product> Products => _Products;
    }
}

ProductSummary code:​

C#:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ViewComponentsdemo.Models;

namespace ViewComponentsdemo.Components
{
    public class ProductSummary : ViewComponent
    {
        private Productsdata data;
        public ProductSummary(Productsdata pdata)
        {
            data = pdata;

        }
        public string Invoke()
        {
            return $"{data.Products.Count()} Products, "
            + $"{data.Products.Sum(c => c.Cost)} Worth of Stock";

        }


    }
}
 

Attachments

  • Screenshot 2021-09-28 182508.png
    Screenshot 2021-09-28 182508.png
    162.5 KB · Views: 75
Last edited by a moderator:
Please put your code in code tags.
 
Where is your code where you register all your components into your inversion of control manager? The error is pretty clear that it's unable to resolve the type. The usual cause of that error is failing to register the type. The next most common cause of the error is a typo (e.g. upper case vs lower case), but most modern IoC frameworks take an actual type and let the compiler do the checking instead of taking a string where a user can fat finger a character.
 
Where is your code where you register all your components into your inversion of control manager? The error is pretty clear that it's unable to resolve the type. The usual cause of that error is failing to register the type. The next most common cause of the error is a typo (e.g. upper case vs lower case), but most modern IoC frameworks take an actual type and let the compiler do the checking instead of taking a string where a user can fat finger a character.
Yes,I fixed it,it was the lowercase problem,stupid me.Thank you very much
 
Back
Top Bottom