ASP.NET MVC使用数据库表跨多个向导步骤持久保存数据 [英] ASP.NET MVC persisting data across multiple wizard steps using database tables

查看:164
本文介绍了ASP.NET MVC使用数据库表跨多个向导步骤持久保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC的多个向导步骤中,使用数据库表实现数据持久性的最佳方法是什么?

What is the is the best approach to implement data persistence using database tables over several wizard steps in ASP.NET MVC?

目前,我们正在使用一个会话来保留多个向导步骤/视图中的大量数据.我们遇到了一个问题,我们怀疑应该归咎于会话超时.因此,我们决定用数据库表替换会话.

At the moment we are using a session to persist alot of data across several wizard steps/views. We are experiencing an issue and we suspect that session timeouts are to blame. For this reason we have decided to replace sessions with database tables.

到目前为止,我们已经确定需要以下内容:

So far we've established that we need the following:

  • 当用户点击首页时,会有一个唯一的ID/令牌(可能是 数据库主键)将确定数据在哪里 存储在整个工作流程中.该ID/令牌使用 网址(如果可能的话),这样我们就无需重新实现会话

  • When the user hits the first page a unique id/token (possibly database primary key) is generated that will determine where data is stored throughout the workflow. This id/token is persisted using the URL, if at all possible, so that we don't need to re implement sessions

每个向导视图/步骤的单独表.我们在每个操作中都实现了逻辑,以测试存储在会话中的向导步骤对象,以确保用户不能跳过工作流中的步骤.通过将数据分为不同的表而不是单个表,可以更轻松地实现具有数据库数据持久性的类似测试

Separate tables for each wizard view/step. We have implemented logic in each action that test wizard step objects stored in the session to ensure that the user can't skip steps in the workflow. Implementing similar testing with database data persistence would be easier with data separated into different tables as opposed to a single table

将过期时间戳存储在与提供的id/令牌关联的记录中的某个位置,以模仿会话超时,例如,如果当前日期时间大于所存储的日期时间戳,则在处理发布的表单时拒绝请求

Storing an expiration time stamp somewhere in the record associated with the provided id/token as to imitate session timeouts e.g when processing the posted form if the current date time is greater than the stored date time stamp deny the request

使用实体框架推送和提取数据

Use entity framework to push and pull data

我们很难弄清楚如何在代码中实现这一点.我们遇到了 http://www.4guysfromrolla.com/webtech/041600-2.shtml 有点帮助,但并没有真正解释如何在ASP.NET MVC控制器中实现它.

We are just having a hard time figuring out how to implement this in code. We came across http://www.4guysfromrolla.com/webtech/041600-2.shtml which was somewhat helpful but didn't really explain how to implement it in an ASP.NET MVC controller.

我们在下面提供了一段代码,以了解我们当前如何使用会话来做事:

We have provided a snippet of code below to provide some understanding of how we are currently doings things using sessions:

控制器

[HttpGet]
public ActionResult StepOne() {
    StepOneViewModel stepOneModel;
    WizardViewModel wizard = (WizardViewModel)Session["Wizard"];

    if(wizard == null || wizard.StepOne == null)
        stepOneModel = new StepOneViewModel();

    else
        stepOneModel = wizard.StepOne 

    return View();
}

[HttpPost]
public ActionResult StepOne()
{
    //validate and store data into wizard session object
}

public ActionResult StepTwo()
{
    StepTwoViewModel stepTwoModel;
    WizardViewModel wizard = (WizardViewModel)Session["Wizard"];

    if(wizard == null || wizard.StepOne == null)
        return RedirectToAction("StepOne");

    if(wizard.StepTwo == null)
        stepTwoModel = new StepTwoViewModel();

    else
        stepTwoModel = wizard.StepTwo;

    Session["Wizard"] = wizard;

    return View();
}

向导模型

public WizardViewModel
{
   public StepOne { get; set; }
   public StepTwo  { get; set;}
}

实施此方法的最佳方法是什么?我们如何创建/跟踪这个确定从何处提取数据的唯一令牌?我们如何测试用户已经完成了先前的步骤并且没有试图跳过?任何关于如何实现此方法的控制器代码/想法都将受到赞赏.

What is the best way to implement this approach? How do we create/track this unique token that determines where data is pulled from? How do we test that users have completed previous steps and not tried to skip? Any controller code/thoughts on how we can implement this approach are appreciated.

推荐答案

这是我们过去实现类似模式的方式. 我在这里假设您的用户未在系统中注册(如果这样的话,这会容易一些)

Here is how we have implemented a similar pattern in the past. I am assuming here that your users are not registered in the system (if they are then it's a bit easier)

  1. 向导的第一步是收集用户的电子邮件地址.

  1. First step on the wizard is to gather the users email address.

我们为此会话生成一个唯一的令牌,并将其嵌入到url中并将其通过电子邮件发送给用户.用户可以使用它随时返回并完成工作流程.您需要使令牌足够大,以使人们无法猜测随机令牌.

We generate a unique token for this session and embed it into url and email it to the user. This can be used by the user to come back at any time and complete the workflow. You need to make the token sufficiently large so people cant just go guessing random tokens.

注意:我们实际上生成了一个令牌ID,我们在内部使用它来映射到工作流程.我们还会生成一个嵌入到url中并发送给用户的哈希.

Note: We actually generate a Token Id which we use internally to map to the workflow process. We also produce a hash that is embedded into the url and sent to the user.

  1. 我们使用正常的实体表,而不是每个步骤都使用单独的表,在这些表的顶部,我们还有一个表,该表将用户令牌链接到实体数据并指示状态,我们使用该表来确定他们在向导中走了多远.这意味着您拥有一条基本路线,该路线确定了当前步骤并转移到该步骤的正确路线.

  1. Rather than separate tables for each step, we use our normal entity tables and over the top of those, we have a table that links the user token to the entity data and indicates a state, which we use to determine how far through the wizard they are. That means you have a base route, that determines the current step and transfers to the correct route for that step.

例行清理令牌,即删除2周以上的令牌.通过电子邮件发送给用户的url仍然有效,但是将其带到新的工作流程中,并且令牌被重新用于新的会话.

The tokens are routinely cleaned up, i.e. tokens older than 2 weeks are removed. The url the user was emailed still works but takes them to a new workflow, and the token is reused for a new session.

最后,如果工作流程完成,我们可以从系统中删除令牌.

Finally if the workflow is completed, we can delete the token from the system.

如果用户已注册到系统中,则无需通过电子邮件向他们发送电子邮件,只需以某种方式将工作流链接到该用户.

If the users are registered into the system, then you don't need to email them, you just need to link your workflow to the user somehow.

这篇关于ASP.NET MVC使用数据库表跨多个向导步骤持久保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆