2

this error appears when I try to access the user's profile logged by the layout. I want to see the details of a specific user, edit it or delete it so I need to send the id to the view, I am trying to do it using this function in the controller:

public IActionResult PerfilSocio( HttpContext contexto,int? id)
{
    int x = Convert.ToInt32(contexto.Session.GetInt32("UserId"));

    if (id == x)
    {

        foreach (var item in _context.Socios)
        {
            if (item.Idsocio == x)
            {
                var y = item;
                return View(y);

            }
        }

    }
    else
    {
        return View("NaoEncontrado");
    }
    return View();
}

In the view I have this:

    @model WebApplication1.Models.Socios

@{
    ViewData["Title"] = "PerfilSocio";
}

<style>
    body {
        padding-top: 0px;
        background-color: gray;
        background-image: url();
        background-image: url();
        background-repeat: no-repeat;
    }
</style>
<h1 style="color:white;"> <b>LabGym</b> </h1>
<p style="text-align:right;color:white;">
    <i class="material-icons">&#9997;</i>
    <a href="~/Mensagems/VerMensagem"> Mensagens </a>
</p>
<br />

<div>

    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            @*@Html.DisplayNameFor(model => model.Fotografia)*@
        </dt>
        <dd class="col-sm-10">
            <img src="~/Fotos/ + model.Fotografia " />
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Email)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Email)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Telefone)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Telefone)
        </dd>

        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Sexo)
        </dt>
        <dd class="col-sm-10">
            @*@Html.DisplayFor(model => model.Sexo)*@
            @if (@Html.DisplayNameFor(model => model.Sexo) == "0")
            {
                <p>Feminino</p>
            }
            else
            {
                <p>Masculino</p>
            }
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Altura)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Altura)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.NomeUtilizador)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.NomeUtilizador)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.PesoInicial)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.PesoInicial)
        </dd>

        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Estado)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Estado)
        </dd>
    </dl>
</div>
<div>
    <a asp-action="Edit" asp-route-id="@Model.Idsocio">Editar perfil</a> |

</div>
<br />
<br />
<br />
<br />

Can someone help me correct this error?

InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Http.HttpContext'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, give the 'contexto' parameter a non-null default value.

2 Answers 2

1

Do not accept HttpContext in your action as a parameter, instead do this.

public IActionResult PerfilSocio(int? id)
{
    int x = Convert.ToInt32(HttpContext.Session.GetInt32("UserId"));
    if (id == x)

HttpContext is just the current HttpContext exposed to you by the Controller class.

If not already added you will need to do the following change in the startup.cs file

Add related services to ConfigureServices() method.

services.AddDistributedMemoryCache();
services.AddSession();

Add UseSession to Configure() method as one of the first statements.

app.UseSession();
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thank you for your answer I corrected as indicated, and it is already working but it returns me the view saying that the user was not found. He should send the Id of the user who logged in in order to consult his profile with the details
how do i solve this? I am using a user registered in the database, and the "Profile" field only appears when the user is logged in as a partner
0

As the error message is telling you, ASP.NET will try to model bind any parameters your action method takes (i.e. mapping them from URL parameters, HTTP request body, etc).

The HttpContext type is setup by the ASP.NET core infrastructure itself, and so you cannot access it by just making it a parameter of your action. If your controller inherits from the Controller class then the HttpContext is available as a property on it, so you can just access it directly from within your action method:

Convert.ToInt32(HttpContext.Session.GetInt32("UserId"))

If for some reason you're not inheriting from the Controller type, or you want to access the HttpContext from outside of a controller, then you can inject an IHttpContextAccessor into the class:

private readonly IHttpContextAccessor httpContextAccessor;

public MyController(IHttpContextAccessor httpContextAccessor)
{
    this.httpContextAccessor = httpContextAccessor;
}

public IActionResult PerfilSocio( HttpContext contexto,int? id)
{
    int x = Convert.ToInt32(httpContextAccessor.HttpContext.Session.GetInt32("UserId"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.