简体   繁体   English

ASP.NET MVC WEB API

[英]ASP.NET MVC WEB API

New to MVC. MVC的新手。 I did the tutorial @ [ http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-(spa)-with-aspnet-web-api-and-angularjs] and from this you produce a question and answer website. 我做了本教程@ [ http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-(spa)-with- aspnet-web-api-and-angularjs],并由此产生一个问答网站。 If I wanted to maintain progress ie keep a count of the number of questions correctly answered, do I need to calculate this value from retrieving the db.TriviaAnswers object or do I need to add a Count property to the TriviaAnswer class or do I need a separate variable then how do I maintain state between requests? 如果我想保持进度,即保持对正确回答的问题数的计数,我是否需要通过检索db.TriviaAnswers对象来计算该值,或者是否需要向TriviaAnswer类添加Count属性,或者是否需要单独的变量,然后如何维护请求之间的状态? Like ViewBag is not available in the 像ViewBag在

public async Task<IHttpActionResult> Post(TriviaAnswer answer){...}

method. 方法。

OPTION 1 as suggested below: 建议1,如下所示:

namespace GeekQuiz.Models
{
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using Newtonsoft.Json;

    public class TriviaResults
    {
        [Required, Key, Column(Order=1)]
        public string UserId { get; set; }

        [Required, Key, Column(Order=0)]
        public virtual int QuestionId { get; set; }
    }
}

This code throws an InvalidOperationException in the method: 这段代码在方法中抛出InvalidOperationException:

private async Task<TriviaQuestion> NextQuestionAsync(string userId)

on the first line of code. 在代码的第一行。

lastQuestionId = ...

I went over this tutorial a few months ago. 几个月前,我阅读了本教程。

option 1: If you want to track progress I assume you mean progress per user, then I would advice you to add a table to the db which states saves the users ids and the ids of questions which were correctly answered - that's in case you want to save this as a persistent data and per user. 选项1:如果您想跟踪进度,我假设您的意思是每位用户的进度,那么我建议您将一个表添加到数据库中,该表会保存用户ID和正确回答的问题的ID-在这种情况下,您可以将其保存为持久性数据和每个用户。

option 2: If you want the same thing, save the data per user but only for this session, you can save the data in the session variable as a dictionary<userid, list<questionid>> . 选项2:如果您想要相同的内容,请为每个用户保存数据,但仅为此会话保存,您可以将数据作为dictionary<userid, list<questionid>>保存在会话变量中。

One thing you should notice is that those question repeat in an endless loop, so you might want to change that. 您应该注意的一件事是,这些问题以无休止的循环重复进行,因此您可能需要更改它。

In both options when you need to know the count u can just go to the table or dictionary and get the number of correct answers. 在这两个选项中,当您需要知道计数时,您都可以转到表格或字典并获得正确答案的数量。

I hope that answers your question. 我希望能回答您的问题。

To use the session var: 要使用会话变量:

Session["name"] = value;
Session.Remove("name");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM