asp.net ActionResult无法在控制器中看到静态数据 [英] asp.net ActionResult cant see static data in controller

查看:154
本文介绍了asp.net ActionResult无法在控制器中看到静态数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的简单静态类

I have a simple static class like this

public static class pdfDataSingle
{
    public static PdfData pdf { get; private set; }

    public static void SetData(PdfData data)
    {
        pdf = data;
    }
}

我首先在MVC控制器中这样设置类

I set the class first like this in the MVC controller first

pdfDataSingle.SetData(pdfData);

我已经确认数据全部存在.

I have confirmed the data is all there..

然后我需要从操作结果中获取字节,所以我先打电话

I then need to get the bytes from an action result, so first i call

 pdf.Add(CreateCertificatePart(CertificatePart.NewStatementOfFact));

所以看起来像这样.

private byte[] CreateCertificatePart(CertificatePart part)
    {
        var customSwitches = string.Format(
            "--print-media-type " +
            "--margin-top 10mm " +
            "--margin-bottom 10mm " +
            "--margin-left 10mm " +
            "--margin-right 10mm " +
            "--encoding utf-8 " +
            "--minimum-font-size 11 " +
            "--zoom 1.0 " +
            "--disable-smart-shrinking"
        );

        var pdfResult = new ActionAsPdf(part.ToString()) { CustomSwitches = customSwitches };

        var pdfBytes = pdfResult.BuildFile(ControllerContext);

        return pdfBytes;
    }

被调用的动作方法看起来像这样..但是静态数据pdfDataSingle为空!

The action method that gets called looks like this.. but the static data pdfDataSingle is null!

public ActionResult NewStatementOfFact()
    {
        try
        {

            AppInsightLog.LogInfo(pdfDataSingle.pdf.Schedule.PolicyNumber, "NewStatementOfFact");
            return View("StatementOfFact", pdfDataSingle.pdf.Schedule);
        }
        catch (Exception ex)
        {
            //AppInsightLog.LogError(ex, quoteReference);
            return new HttpStatusCodeResult(400, "NewStatementOfFact");
        }
    }

当我在本地运行它时效果很好..操作结果看到了静态类中的数据(在我的控制器外部声明),但是当发布为天蓝色时,它们却没有..任何人都不知道为什么会这样吗?

This works fine when I run it locally.. the action results see the data in the the static class (declared outside my controller), but when published to azure they dont.. anyone have any idea why this might be?

谢谢

推荐答案

将数据(PdfData)序列化为json字符串,然后将其保存到数据库中.然后,您可以在操作方法中将其读回并反序列化.

Serialise the data (PdfData) into a json string and save that to a database. You can then read it back in the action methods and de-serialise it.

这篇关于asp.net ActionResult无法在控制器中看到静态数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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