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

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

问题描述

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

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.

我不知道为什么会发生这种情况,因为我对 MVC 技术还很陌生.

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..

在 Chris 建议之后,我在方法的顶部添加了 [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!");
}

编辑编辑

有趣的事实是,无论我在我的控制器方法中写了什么,即使我注释了所有代码,它仍然会在视图上中断,这意味着它永远不会进入控制器的方法.我在 UnReadEmails 方法的开头放置了 breakpoing 并且它从未被命中,它总是在视图上中断!

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 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).

在您的实际子操作代码中,您没有返回重定向,这意味着您必须将操作过滤器应用于发出重定向的子操作.特别是,您应该避免在子操作上使用诸如 AuthorizeRequireHttps 之类的东西,因为它们通过使用重定向来工作,而这又是子操作无法做到的.如果子操作位于使用 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.

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

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