Asp.net core 如何使用TempData属性将数据传递到Razor视图

Asp.net core 如何使用TempData属性将数据传递到Razor视图,asp.net-core,razor,Asp.net Core,Razor,我有: 在Startup.cs中: namespace Test { public interface ITest { public string Test1(string s); } public class Test : ITest { [Microsoft.AspNetCore.Mvc.TempData] public string Message { g

我有:

在Startup.cs中:

namespace Test
{
    public interface ITest
    {
        public string Test1(string s);
    }

    public class Test : ITest   
    {
        [Microsoft.AspNetCore.Mvc.TempData]
        public string Message
        {
            get; set;
        }
        public string Test1(string s)
        {
            Message = "Test " + s;
            return "Test has run";
        }
    }
}
我哪里做错了?如何将
TempData[“Message”]
从某些代码(不在控制器中)传递到Razor页面

如何将TempData[“Message”]从某些代码(不在控制器中)传递到Razor页面

如果希望在自定义服务内保留并通过传递值,可以尝试以下代码段

Result is Test has run

TempData["Message"] is
公共接口测试
{
公共字符串Test1(字符串s);
公共字符串Test2();
}
公共类考试:ITest
{
专用只读IHttpContextAccessor\u httpContextAccessor;
公共测试(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor=httpContextAccessor;
}
公共字符串消息
{
得到{
var tempDataDictionaryFactory=_httpContextAccessor.HttpContext.RequestServices.GetRequiredService();
var tempDataDictionary=tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
if(tempDataDictionary.TryGetValue(“消息”,输出对象值))
{
返回(字符串)值;
};
返回“”;
}
设置
{
var tempDataDictionaryFactory=_httpContextAccessor.HttpContext.RequestServices.GetRequiredService();
var tempDataDictionary=tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
tempDataDictionary.Remove(“消息”);
tempDataDictionary.TryAdd(“消息”,值);
}
}
公共字符串Test1(字符串s)
{
Message=“Test”+s;
返回“测试已运行”;
}
公共字符串Test2()
{
返回消息;
}
}
翻页

public interface ITest
{
    public string Test1(string s);
    public string Test2();
}

public class Test : ITest
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    public Test(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public string Message
    {
        get {
            var tempDataDictionaryFactory = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
            var tempDataDictionary = tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
            if (tempDataDictionary.TryGetValue("Message", out object value))
            {
                return (string)value;
            };

            return "";
        }
        set
        {
            var tempDataDictionaryFactory = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
            var tempDataDictionary = tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
            tempDataDictionary.Remove("Message");
            tempDataDictionary.TryAdd("Message", value);
        }
    }
    public string Test1(string s)
    {
        Message = "Test " + s;
        return "Test has run";
    }

    public string Test2()
    {
        return Message;
    }
}
结果是@Test.Test1(“你好,世界!”)

TempData[“Message”]是@TempData.Peek(“Message”)

@Test.Test2()

测试结果

如何将TempData[“Message”]从某些代码(不在控制器中)传递到Razor页面

如果希望在自定义服务内保留并通过传递值,可以尝试以下代码段

Result is Test has run

TempData["Message"] is
公共接口测试
{
公共字符串Test1(字符串s);
公共字符串Test2();
}
公共类考试:ITest
{
专用只读IHttpContextAccessor\u httpContextAccessor;
公共测试(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor=httpContextAccessor;
}
公共字符串消息
{
得到{
var tempDataDictionaryFactory=_httpContextAccessor.HttpContext.RequestServices.GetRequiredService();
var tempDataDictionary=tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
if(tempDataDictionary.TryGetValue(“消息”,输出对象值))
{
返回(字符串)值;
};
返回“”;
}
设置
{
var tempDataDictionaryFactory=_httpContextAccessor.HttpContext.RequestServices.GetRequiredService();
var tempDataDictionary=tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
tempDataDictionary.Remove(“消息”);
tempDataDictionary.TryAdd(“消息”,值);
}
}
公共字符串Test1(字符串s)
{
Message=“Test”+s;
返回“测试已运行”;
}
公共字符串Test2()
{
返回消息;
}
}
翻页

public interface ITest
{
    public string Test1(string s);
    public string Test2();
}

public class Test : ITest
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    public Test(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public string Message
    {
        get {
            var tempDataDictionaryFactory = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
            var tempDataDictionary = tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
            if (tempDataDictionary.TryGetValue("Message", out object value))
            {
                return (string)value;
            };

            return "";
        }
        set
        {
            var tempDataDictionaryFactory = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>();
            var tempDataDictionary = tempDataDictionaryFactory.GetTempData(_httpContextAccessor.HttpContext);
            tempDataDictionary.Remove("Message");
            tempDataDictionary.TryAdd("Message", value);
        }
    }
    public string Test1(string s)
    {
        Message = "Test " + s;
        return "Test has run";
    }

    public string Test2()
    {
        return Message;
    }
}
结果是@Test.Test1(“你好,世界!”)

TempData[“Message”]是@TempData.Peek(“Message”)

@Test.Test2()

测试结果

<p>Result is @Test.Test1("Hello World!")</p>
<p>TempData["Message"] is  @TempData.Peek("Message")</p>

<p>@Test.Test2()</p>