ASP.NET Razor Pages error processing request

2

I'm implementing an example using ASP.NET Razor Pages and when trying to access the Index page I get the error that hears error in the request.

  

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

     

Development Mode Swapping to Development environment will display more   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()
        {

        }
    }
}
    
asked by anonymous 20.12.2017 / 15:53

2 answers

2

The error occurs because Razor Page can not know at runtime which of the two GET methods created in your code , that is, OnGet and OnGetAsync is to be executed, summing up can not coexist both at the same time in code, because they do the same thing, generating an exception because of that.

20.12.2017 / 16:21
0

No web.config change (or add if it does not exist) the value of tag environmentVariable to 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: link

If you do not resolve, just let us know.

    
20.12.2017 / 16:04