MVC 5想要导出到PDF导出但登录页面

时间:2016-12-12 05:00:48

标签: asp.net-mvc pdf asp.net-mvc-5

这应该是expoert的行动: 1- visual studio 2015 mvc 5和安装包 Rotativa for PDF

        [Authorize(Roles = "admin,operator")]
        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }

up动作有责任选择一些值并在视图中显示,视图是部分

这可以查看:

@model IEnumerable<ProjectOne.Models.Student>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.StatusName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>

    </tr>
}
</table>
@Html.ActionLink("Export", "ExportIndex")    //this will run ExportIndex

这是ExportIndex:

    [Authorize(Roles = "admin,operator")]
    public ActionResult ExportIndex(int ? id)
    {
        return new ActionAsPdf("Index") { FileName = "Test.pdf" };
    }

现在在导出问题之后它会创建pdf但是登录屏幕pdf不会出现什么问题,即使我使用相同的角色用户登录,如果我将授权更改为[AllowAnonymous]它会工作但是我需要权限这是在应用角色时将导出为pdf的页面。

image is

1 个答案:

答案 0 :(得分:0)

您需要在ActionAsPdf object intializer

中加入Cookie
[Authorize(Roles = "admin,operator")]
public ActionResult ExportIndex(int?id)
{
    var cookies = Request.Cookies.AllKeys.ToDictionary(k => k, k => Request.Cookies[k].Value); 
    return new ActionAsPdf("Index")
    {
        FileName = "Test.pdf", 
        FormsAuthenticationCookieName = System.Web.Security.FormsAuthentication.FormsCookieName, 
        Cookies = cookies 
    }; 

}