Hi @Cloud Developer ,Welcome to Microsoft Q&A,
If there are only two possible categories, you can also use try catch to parse both strings, and then perform another processing method on failure. This ensures that no matter what the input string is, it will be processed.
public async Task<ActionResult> OnGetModels(int modelId)
{
var carModel = await _db.CarModels.FirstOrDefaultAsync(x => x.id == modelId);
if (carModel != null)
{
IEnumerable<string> output;
try
{
output = ParseOutput(carModel.Details);
}
catch
{
output = ShowDetails(carModel.Details);
}
return new JsonResult(output);
}
else
{
return NotFound();
}
}
public IEnumerable<string> ParseOutput(string outputValues)
{
// Try to parse the string here, if the parsing fails, throw an exception
// Otherwise, remove the leading X and split into { ; }, then return the result
}
public IEnumerable<string> ShowDetails(string outputValues)
{
// Perform other formatting or processing here and return the result
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.