result.viewname 始终为 string.empty

发布于 2024-08-11 22:17:17 字数 530 浏览 3 评论 0原文

我似乎无法返回 result.ViewName 用于 Nunit 测试,因为它总是返回 string.empty。我已在控制器内明确设置了视图的名称,并希望测试能够识别该名称。我四处寻找,似乎如果我明确设置它,我应该找回视图名称。有人有什么想法吗?

public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View("Index");
    }
}

我的测试看起来像这样

    [Test]
    public void TestIndexView()
    {
        var controller = new HomeController();
        var result = controller.Index() as ViewResult;
        Assert.AreEqual("Index", result.ViewName);
    }

I cannot seem to return the result.ViewName for use in Nunit tests as it always returns string.empty. I have explicitly set the name of the view inside my controller and would expect the test to pick this up. I have had a hunt around and it seems that I should get the Viewname back if I set it explicitly. Any one got any ideas?

public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View("Index");
    }
}

My test looks like this

    [Test]
    public void TestIndexView()
    {
        var controller = new HomeController();
        var result = controller.Index() as ViewResult;
        Assert.AreEqual("Index", result.ViewName);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-08-18 22:17:17

您是否尝试过清理和重建解决方案?它应该可以正常工作。

Did you try cleaning and rebuilding solution? It should work without problems.

趴在窗边数星星i 2024-08-18 22:17:17

您需要return new View("Index");。如果这是 C 语言,原因是您在 Index() 中创建 View 的方式,它只是存储在堆栈上并超出范围(并且因此在函数结束时被收集)。这会导致 C 崩溃,但 C# 在这方面似乎更聪明一些。

You need to return new View("Index");. If this were C the reason why is because of the way you're creating the View in Index() it is just stored on the stack and goes out of scope (and thus gets collected) when the function ends. This would cause C to crash, but C# appears to be a little smarter in this regard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文