ASP.NET Razor Pages error while processing the request

Asked

Viewed 266 times

2

I’m implementing an example using ASP.NET Razor Pages and when trying to access the page Index I have the error that there is error in the request.

Error. An error occurred while Processing your request. Request ID: 0HLA7N3BM0ANB:00000001

Development Mode Swapping to Development Detailed information about the error that occurred.

Development Environment should not be enabled in Deployed Applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, Development Environment can be enabled by Setting the ASPNETCORE_ENVIRONMENT Environment variable to Development, and restarting the application.

Index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<h1>Contacts</h1>
<form method="post">
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var contact in Model.Customers)
            {
                <tr>
                    <td>@contact.Id</td>
                    <td>@contact.Name</td>
                    <td>
                        <a asp-page="./Edit" asp-route-id="@contact.Id">edit</a>
                        <button type="submit" asp-page-handler="delete" 
                                asp-route-id="@contact.Id">delete</button>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <a asp-page="./Create">Create</a>
</form>

Index.cshtml.Cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesContacts.Data;
using Microsoft.EntityFrameworkCore;

namespace RazorPagesContacts.Pages
{
    public class IndexModel : PageModel
    {
        private readonly AppDbContext _db;

        public IndexModel(AppDbContext db)
        {
            _db = db;
        }

        public IList<Customer> Customers { get; private set; }


        public async Task OnGetAsync() 
        {
            Customers = await _db.Customers.AsNoTracking().ToListAsync();   
        }

        public async Task<IActionResult> OnPostDeleteAsync(int id)
        {
            var contact = await _db.Customers.FindAsync(id);
            if(contact != null)
            {
                _db.Customers.Remove(contact);
                await _db.SaveChangesAsync();
            }

            return RedirectToPage();
        }

        public void OnGet()
        {

        }
    }
}
  • In configure you have this if if (env.IsDevelopment())&#xA; {&#xA; app.UseDeveloperExceptionPage();&#xA; }

  • Do you have that if

  • Already debugged? when the error happens?

  • has two Onget? can not! IE, Onget and Ongetasync does the same thing and the error happens there, so comment Onget

  • Even one being Async?

  • Yes ... !!! even though Async and even having another subscription... Razor Pages has these inconveniences

  • The logic is: it has to execute only one of the methods? as the two Razor Pages does not know which to execute and ai the exception.

  • ?Worked? ....

  • 1

    It worked, yeah, thanks

Show 4 more comments

2 answers

2


0

In the web.config change(or add if not available) the tag’s value environmentVariable for Development:

<environmentVariables>
        <environmentVariable name = "ASPNETCORE_ENVIRONMENT" value="Production" />
</environmentVariables>



<configuration>
  <!--
    Configure your application settings in appsettings.json.Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name = "aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath = ".\Application.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name = "ASPNETCORE_ENVIRONMENT" value="Production" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

Source: https://stackoverflow.com/questions/39737585/asp-net-core-deployment-to-iis-error-development-environment-should-not-be-enab

In case you don’t decide, just let me know.

  • I don’t have a web.config in my project, only appsettings.json.

  • 1

    I am using the vscode and created the project using the new Razor dotnet

  • I created in another question about how to place in Development https://answall.com/q/264319/5846

Browser other questions tagged

You are not signed in. Login or sign up in order to post.