【问题标题】:MVC Data being visible to other clientsMVC 数据对其他客户端可见
【发布时间】:2019-03-24 04:49:19
【问题描述】:

我对 MVC 的经验很少,所以我边走边学,遇到了一些绊脚石。

我已经开始开发 MVC Web 应用程序,并且刚刚完成了一个表单,该表单调用 Web 服务以获取地址列表并使用每个地址填充选择中的选项。

这很好用,但是我已将其部署到测试服务器,并且当我运行该站点时它可以工作,但是如果我的另一个客户端的同事访问该页面,它会显示我运行的数据列表。对网站的每次调用都应该是单独的,所以不确定为什么其他人可以看到这些数据?

查看(index.cshtml)

namespace AddressLookupSite.Controllers
{
public class AssistedController : Controller
{
    string postcode = "";
    string street = "";
    //model is the list of addresses to be returned to the user
    private static AddressList model = new AddressList();

    // GET: Assisted
    public ActionResult Index()
    {
        //this may need to be index,models.
        return View(model);
    }

    public ActionResult GetAddresses(string postcode)
    {
        if (postcode == null || postcode == "")
        {
            return RedirectToAction("/Index/");
        }
        //call Addresslookup web service
        AddressLookupWeb ew = new AddressLookupWeb();
        //extract address values from the XML returned from web service
        XmlNode xml = ew.GetAddress(", , , , " + postcode);

        foreach (XmlElement addressInfo in xml)
        {
            foreach (XmlElement teset in addressInfo["Addresses"])
            {
                //add each address item found to the list
                model.listone.Add(new AddressResults { FullAddress = teset["fulladdress"].InnerText });
            }
        }

        //return the list and model back to the index view
        return View("Index", model);


    }
}

模型(地址结果)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AddressLookupSite.Models
{
public class AddressResults
{
    public string FullAddress { get; set; }
}
}

模型(地址结果)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AddressLookupSite.Models
{
public class AddressList
{
    public List<AddressResults> listone = new List<AddressResults>();
}
}

视图非常标准,访问模型,将模型的内容输出到select,它还使用一些JavaScript来做一些基本的代码。

问题肯定是服务器端的,它不应该让其他用户能够拦截表单,特别是因为表单稍后会包含个人数据。这让我想到,如果我正在填写表格并且其他人打开了页面,那么当它不应该发生时,这会给他们来自控制器的数据。

非常感谢任何建议,因为我说我是新手,以前只开发内部应用程序来运行流程等。

【问题讨论】:

    标签: c# asp.net model-view-controller web-applications controller


    【解决方案1】:

    您将结果模型定义为static 并内联赋值:

    //model is the list of addresses to be returned to the user
    private static AddressList model = new AddressList();
    

    这意味着这个类只有一个实例。这似乎是每个人都得到相同结果的原因。

    删除静态或在将使用它的方法中构造实例。要么给每个调用者自己的副本。

    【讨论】:

    • 如果我取出静态文件,这是否意味着特定用户如果返回主页会丢失他们的数据?一旦用户提交表单,他们就会被带到 /assistance/getAddresses 但我想要的方式是,如果他们要导航回 /assisted/ 表单仍然会有他们的数据。
    • PS 感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2012-05-14
    • 2019-10-23
    相关资源
    最近更新 更多