"无法找到资源"来自ActionResult

时间:2015-03-13 19:06:05

标签: c# asp.net-mvc

我已在生成excel文件的控制器中生成了一个动作结果,并且我通过@ url.Action帮助将其传递给按钮中的View但是当我点击按钮时它表示:

  

' /'中的服务器错误应用

     

无法找到资源。

     

描述:HTTP 404.您正在查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请重新注意以下网址,并确保拼写正确。

     

请求的网址:/ Report / PinpointReport

Controller.cs代码

public ActionResult PinpointReport(DateTime Period)
{
    try
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes("Test")
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "test,xlsx");
    }
    catch(Exception ex)
    {
        ViewBag.ErrorMessage = "An error occurred loading the report: " + ex.Message;

        string errorString = ex.ToString();
        return Json(new
        {
            error = errorString
        });
    }
}

Reports.cshtml代码

这是我调用我的行动结果

的地方
<input type="button" value="View Total Revenue" onclick="location.href='@Url.Action("PinpointReport", "Report")'" />

public ActionResult ExportToExcel()
{
    var products = new System.Data.DataTable("test");
    products.Columns.Add("col1", typeof(int));
    products.Columns.Add("col2", typeof(string));

    products.Rows.Add(1, "product 1");
    products.Rows.Add(2, "product 2");
    products.Rows.Add(3, "product 3");
    products.Rows.Add(4, "product 4");
    products.Rows.Add(5, "product 5");
    products.Rows.Add(6, "product 6");
    products.Rows.Add(7, "product 7");


    var grid = new GridView();
    grid.DataSource = products;
    grid.DataBind();

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xlsx");
    Response.ContentType = "application/vnd.ms-excel";

    Response.Charset = "";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();

    return View("Reports");
}

1 个答案:

答案 0 :(得分:1)

在你的评论中,你没有在你的行动中传递DateTime参数,试试这个:

<input type="button" value="View Total Revenue"onclick="location.href='@Url.Action("PinpointReport", "Report", new { Period = 'some date' })'" />
相关问题