我不想使问题过于复杂,但是这里有……我有一个问题和答案文件。我想使用 C# ASP.NET Core MVC 从文件中获取问题,格式为 JSON,使用 AJAX 将问题值返回到网页。通过单击一个按钮来显示问题,该按钮通过使用 Route[] 语句连接到它来运行 C# 方法。然后,同样在同一网页上的 AJAX 中,用户可以提交他们的答案。如果他们是正确的,它会告诉他们。如果不涉及随机数,则此方法有效。随机数只是选择包含问题的文本文件的行 - 这就是它的全部!但是,catch-22 是当用户单击“ANSWER”按钮提交答案时,它会重新运行 C# 方法,该方法会生成一个新的随机数,因此很有可能从文件,
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () { //When document loads, function is ready (this includes ALL of the functions below!)
$('#showQ').click(function () {
//Block of code that uses AJAX to DISPLAY questions from Controllers/DemoController.cs/answerQuestion()
$.ajax(
{ //Start of the actual AJAX request code...
type: 'GET', //Use GET method to send values
url: '/demo/answerQuestion/',//The location of the method (Controllers/DemoController.cs/answerQuestion)
success: function (result) { //If function is successful, it will display the result
$('#displayQ').html(result); //If the code above is successful, it will display the question inside id="displayQ"
}
}
)
});
//Block of code that uses AJAX to ANSWER questions from Controllers/DemoController.cs/answerQuestion()
$('#answerQ').click(function () { //When answerQ button is clicked, run this function...
var answer = $('#answer').val(); //var answer = id="answer" field value when answerQ submit button is pressed
$.ajax(
{ //Start of the actual AJAX request code...
type: 'GET', //Use GET method to send values
url: '/demo/answerQuestion/' + answer, //Submit answer value to answerQuestion
success: function (result) {
$('#displayAns').html(result); //If the code above is successful, it will send the answer and display result in id="displayAns"
}
}
)
});
}); //End of $(document).ready(function...
</script>
</head>
<body>
<fieldset>
<legend>Question...</legend>
<br />
<input type="button" value="Click for question" id="showQ" />
<br />
<span id="displayQ"></span>
<br />
Answer: <input type="text" id="answer" />
<br />
<input type="button" value="Click to submit answer" id="answerQ" />
<br />
<span id="displayAns"></span>
<br />
</fieldset>
</body>
</html>
从 AJAX 获取值的 C# 方法...
[Route("answerQuestion")]
[Route("answerQuestion/{answer}")]
public IActionResult AnswerQuestion(string answer)
{
//Need to think of a way to get this to only run when "Next question" button is clicked and not when answer provided!
//Problem is, if I make it so it only runs if "answer" is not set, then it complains that variable is undefined!
//It's a bit of a catch-22!!!
Random rnd = new Random();
int randomNumber = rnd.Next(0, 10);
string path = @"C:\Users\aposi\Desktop\csharpweb\HOWMVCWORKS\quizquestions.txt";
if (System.IO.File.Exists(path))
{
string[] allLines = System.IO.File.ReadAllLines(path);
string randomLine = allLines[randomNumber];
string removedSlashN = randomLine.Replace("\n", "");
string removedWhitespace = removedSlashN.Replace(" ", "");
string[] separatedout = removedWhitespace.Split("|"); //Has to be string array data-type to work with Split() function
string songname = separatedout[0];
char songletter = songname[0];
string artist = separatedout[1];
if (string.IsNullOrEmpty(answer))
{
return new JsonResult("Artist: " + artist + "First letter of songname: " + songletter);
}
else if (answer == songname)
{
return new JsonResult("Well done! You guessed correctly that the song name is: " + songname);
}
else if (answer != songname)
{
return new JsonResult("Sorry! You got it wrong! The song name was... " + songname);
}
else
{
return new JsonResult("Something went wrong!!!");
}
}
else
{
return new JsonResult("No question file found!");
}
}
}
}