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 });
}
}
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();
}
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"
}
}
}
}

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);
}
}
Fehler 500: Interner Server -Fehler. />
Ich kann nicht herausfinden, was das Problem ist. Bitte hilf mir. Danke