by Anonymous » 05 Apr 2025, 21:00
Bitte betrachten Sie diesen einfachen ASP.NET Core 8 -Web -API -Code: < /p>
Code: Select all
using System.Text.Json.Serialization;
using System.Web.Http;
[Route("api/[controller]")]
public class MyController : ApiController
{
[HttpGet]
[Route("getdata")]
public IHttpActionResult GetData(string param1, string param2)
{
if (string.IsNullOrEmpty(param1) || string.IsNullOrEmpty(param2))
{
return BadRequest("Both param1 and param2 are required.");
}
return Ok(new { Message = "Get: Data received", Param1 = param1, Param2 = param2 });
}
}
und dieser Code in Program.cs :
Code: Select all
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseRouting();
app.MapControllerRoute(name: "DefaultApi",pattern: "api/{controller}/{action}/{param1}/{param2}");
app.Run();
}
Hier ist meine Startssee.json
Code: Select all
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:33096",
"sslPort": 44314
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "My",
"applicationUrl": "http://localhost:5246",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "api/my/getdata",
"applicationUrl": "https://localhost:7225;http://localhost:5246",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/my/getdata",
"applicationUrl": "https://localhost:7225",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
When I ran the project, param1 and param2 are null:
And the code executed successfully:
< /p>
Aber ich habe diese Fehlermeldung im Browser erhalten. 'System.ReadOnlySpan`1 [System.Byte]' der Eigenschaft 'Preamble' on typ 'System.Text.Coding' ist für die Serialisierung oder Deserialisierung ungültig, da es sich um einen Zeigertyp handelt, ist eine Ref -Struktur oder enthält generische Parameter, die durch bestimmte Typen nicht ersetzt wurden. />
Code: Select all
string url = "api/my/getdata";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:7225/");
var jsonContent = JsonSerializer.Serialize(new { param1, param2 });
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.GetAsync(url+"?param1=value1¶m2=value2");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response: " + responseBody);
}
else
{
Console.WriteLine("Error: " + response.StatusCode);
}
}
Trotz der erfolgreichen Ausführung des Code habe ich
Fehler 500: Interner Server -Fehler. />
Ich kann nicht herausfinden, was das
Problem ist. Bitte hilf mir. Danke
Bitte betrachten Sie diesen einfachen ASP.NET Core 8 -Web -API -Code: < /p>
[code]using System.Text.Json.Serialization;
using System.Web.Http;
[Route("api/[controller]")]
public class MyController : ApiController
{
[HttpGet]
[Route("getdata")]
public IHttpActionResult GetData(string param1, string param2)
{
if (string.IsNullOrEmpty(param1) || string.IsNullOrEmpty(param2))
{
return BadRequest("Both param1 and param2 are required.");
}
return Ok(new { Message = "Get: Data received", Param1 = param1, Param2 = param2 });
}
}
[/code]
und dieser Code in Program.cs :
[code]public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseRouting();
app.MapControllerRoute(name: "DefaultApi",pattern: "api/{controller}/{action}/{param1}/{param2}");
app.Run();
}
[/code]
Hier ist meine Startssee.json
[code]{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:33096",
"sslPort": 44314
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "My",
"applicationUrl": "http://localhost:5246",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "api/my/getdata",
"applicationUrl": "https://localhost:7225;http://localhost:5246",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/my/getdata",
"applicationUrl": "https://localhost:7225",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
[/code]
When I ran the project, param1 and param2 are null:
[img]https://i.sstatic.net/M6cnSzXp.png[/img]
And the code executed successfully:
< /p>
Aber ich habe diese Fehlermeldung im Browser erhalten. 'System.ReadOnlySpan`1 [System.Byte]' der Eigenschaft 'Preamble' on typ 'System.Text.Coding' ist für die Serialisierung oder Deserialisierung ungültig, da es sich um einen Zeigertyp handelt, ist eine Ref -Struktur oder enthält generische Parameter, die durch bestimmte Typen nicht ersetzt wurden. />[code]string url = "api/my/getdata";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:7225/");
var jsonContent = JsonSerializer.Serialize(new { param1, param2 });
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.GetAsync(url+"?param1=value1¶m2=value2");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response: " + responseBody);
}
else
{
Console.WriteLine("Error: " + response.StatusCode);
}
}
[/code]
Trotz der erfolgreichen Ausführung des Code habe ich
Fehler 500: Interner Server -Fehler. />
Ich kann nicht herausfinden, was das [url=viewtopic.php?t=20324]Problem[/url] ist. Bitte hilf mir. Danke