子操作不允许执行重定向操作。 (使用PartialViews) [英] Child actions are not allowed to perform redirect actions. (Using PartialViews)

查看:299
本文介绍了子操作不允许执行重定向操作。 (使用PartialViews)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用数据库中的一些数据加载部分视图,但是在运行应用程序时出现以下问题:

I'm trying to load my partial view with some data from database, but I'm getting following issue when I run the application:


不允许子操作执行重定向操作。

Child actions are not allowed to perform redirect actions.

我不知道为什么会这样,因为我很漂亮

I don't know why this is happening because I'm pretty new with MVC technology.

这是我在控制器中的 PartialViewResult 方法:

Here is my PartialViewResult method in a controller:

public PartialViewResult UnReadEmails()
{
   if (User.Id != null)
   {
      List<Emails> resultList = EmailController.GetUnreadEmailsByUserId(User.Id);
       return PartialView("~/Views/Emails/_UnReadEmails.cshtml", resultList);
   }
   return PartialView("Error, not found!");
}

这是我的局部视图本身,称为_UnReadEmails(如您所见)我在此处显示有关发件人和电子邮件正文的信息),PartialView正在检索从控制器发送到的电子邮件列表

And here is my partialview itself, it is called _UnReadEmails (as you can see I'm displaying here info about sender and email body), PartialView is retrieving list of Emails that I'm sending to from my Controller

@model IEnumerable<Emails>

foreach (var item in Model)
{
    <li>
        <a>
            <span>
               <span>@item.EmailSender</span>
               <span class="email">
                     @item.Body;
               </span>
        </a>
    </li>
}

尝试以这种方式加载部分视图后:

After I tried to load my partial view on this way:

@ Html.Action( UnreadEmails, Message)

我开始收到我在标题中提到的以下问题,

I started to receive following issue that I mentioned in my Title,

我已经尝试了一些方法来解决此问题,例如更改 @ Html.Action ( UnreadEmails, Message) @ Url.Action( UnreadEmails, Message)等,但是没有解决我的问题。

I already tried few things to solve this like changing @Html.Action("UnreadEmails", "Message") to @Url.Action("UnreadEmails", "Message") etc etc but that didn't solve my issue.

编辑:它总是在此行中断(在视图上):

It allways breaks on this line (on view) :

@ Html.Action( UnreadEmails, Message)

它永远不会在后面添加代码。。

It never goes into code behind..

在克里斯建议之后,我在方法顶部添加了 [AllowAnonymous]

After Chris suggestion I added [AllowAnonymous] on the top of the method:

[AllowAnonymous]
public PartialViewResult UnReadEmails()
{
   if (User.Id != null)
   {
      List<Emails> resultList = EmailController.GetUnreadEmailsByUserId(User.Id);
       return PartialView("~/Views/Emails/_UnReadEmails.cshtml", resultList);
   }
   return PartialView("Error, not found!");
}

编辑编辑编辑:

有趣的事实是,无论我在Controller方法中编写的内容如何,​​即使我注释了所有代码,它仍然会在View上中断,这意味着它将永远不会进入Controller方法中。我在UnReadEmails方法的开头放置了breakpoing,但它从未被使用过,它始终在View上中断!

Interesting fact is that whatever I wrote in my Controller's method and even if I comment all code, it will still break on a View, that means it will never came into a Controller's method. I put breakpoing there at the begining of the UnReadEmails method and it was never hitted, it allways breaks on a View!

推荐答案

The错误非常明显。子操作不能发出重定向。这是因为,虽然子动作看起来像常规动作,但实际上它们是在单独的过程中呈现的,因此无法修改实际的响应(重定向将需要该响应)。

The error is pretty explicit. Child actions cannot issue redirects. This is because, while child actions look like regular actions, they are in fact rendered in a separate process and cannot modify the actual response (which a redirect would require).

在您的实际子操作代码中,您没有返回重定向,因此这意味着您必须将操作过滤器应用于发出重定向的子操作。特别是,您应该避免在子操作上使用诸如 Authorize RequireHttps 之类的东西,因为这些操作通过使用重定向来工作,再次,儿童动作无法执行。如果子操作位于以 Authorize 装饰的控制器中,则应将其标记为 AllowAnonymous

In your actual child action code, you're not returning a redirect, so that means you must have an action filter being applied to the child action that is issuing the redirect. In particular, you should avoid using things like Authorize or RequireHttps on child actions, as those work by using redirects, which again, a child action cannot do. If the child action is in a controller that is decorated with Authorize, it should be marked with AllowAnonymous.

这篇关于子操作不允许执行重定向操作。 (使用PartialViews)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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