DOTNET -Kernanwendung, die 500 interne Serverreaktion nach dem Hosting auf IIS neu beauftragtC#

Ein Treffpunkt für C#-Programmierer
Guest
 DOTNET -Kernanwendung, die 500 interne Serverreaktion nach dem Hosting auf IIS neu beauftragt

Post by Guest »

Ich habe den REST -API -Dienst im Dotnet Core erstellt. Die Anwendungsendpunkte funktionieren einwandfrei, wenn ich aus dem Code ausgeführt werde, aber nachdem ich ihn auf dem IIS -Server veröffentlicht habe, wenn ich versuche, darauf zuzugreifen, gibt es mir die folgende Fehlerantwort < /p>

Code: Select all

{
"StatusCode": 500,
"Message": "An unexpected error occurred. Please try again later.",
"Details": "The exception handler configured on ExceptionHandlerOptions produced a 404 status response. This InvalidOperationException containing the original exception was thrown since this is often due to a misconfigured ExceptionHandlingPath. If the exception handler is expected to return 404 status responses then set AllowStatusCode404Response to true."
}
< /code>
Ich habe nach zu vielen Lösungen gesucht, aber für mich funktioniert nichts.
Ich habe einfach eine Website in IIS erstellt. Seine Anwendungspool hinzugefügt, binden Sie ihn richtig, aber immer noch kein Glück.public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;

public ExceptionHandlingMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context); // Call the next middleware
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}

private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
// Log the exception (optional)
Console.WriteLine($"Exception: {exception.Message}");

var response = new
{
StatusCode = (int)HttpStatusCode.InternalServerError,
Message = "An unexpected error occurred. Please try again later.",
Details = exception.Message // Include more details for development environment
};

context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return context.Response.WriteAsync(JsonSerializer.Serialize(response));
}
}
< /code>
Programm anhängen.cs Code auch < /p>
using Microsoft.EntityFrameworkCore;
using rajasthan_cmo_backend.Controllers;
using rajasthan_cmo_backend.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddHttpClient();
builder.Services.AddControllers();
builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"),
sqlOptions => sqlOptions.EnableRetryOnFailure()));

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Add AutoMapper to the service container

builder.Services.AddSwaggerGen(c =>
{
c.OperationFilter(); //
Enables file upload in Swagger UI
});

// Add CORS service and policy
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()      // Allow requests from any origin
.AllowAnyMethod()      // Allow any HTTP method (GET, POST, PUT, etc.)
.AllowAnyHeader();     // Allow any headers
});
});

var app = builder.Build();

// Ensure database creation and migrations
// using (var scope = app.Services.CreateScope())
// {
//     var dbContext =
scope.ServiceProvider.GetRequiredService();
//     dbContext.Database.Migrate();
// }
app.UseMiddleware();

// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{

}
app.UseSwagger();
app.UseSwaggerUI();

app.UseHttpsRedirection();

// Use the CORS middleware with the "AllowAll" policy
app.UseCors("AllowAll");

app.UseAuthorization();

app.MapControllers();
app.UseStaticFiles();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
//app.UseExceptionHandler("/Error");
app.UseHsts();
}

//app.UseExceptionHandler(new ExceptionHandlerOptions() {
//    AllowStatusCode404Response = true,
//    ExceptionHandlingPath = "/error" });
app.Run();

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post