2011-09-08 59 views
7

Tôi có một phương thức điều khiển trả về khoảng trống vì nó đang xây dựng một báo cáo Excel để người dùng tải xuống. Thư viện bên thứ ba của Excel mà chúng tôi đang sử dụng đang viết thư cho chính thư trả lời. Phương pháp trông giống như sau:Làm cách nào để chuyển hướng đến một Action/Controller khác trong MVC3 từ một phương thức VOID?

[HttpGet] 
public void GetExcel(int id) 
{ 
    try 
    { 
    var report = _reportService.GetReport(id); 
    var table = _reportService.GetReportTable(id); 
    var excelReport = new ExcelReport(table, report.Name); 
    excelReport.DownloadReport(System.Web.HttpContext.Current.Response); 
    } 
    catch (Exception ex) 
    { 
    // This is wrong, of course, because I'm not returning an ActionResult 
    Response.RedirectToRoute("/Report/Error/", new { exceptionType = ex.GetType().Name }); 
    } 
} 

Có một số kiểm tra bảo mật xảy ra nếu người dùng không đáp ứng các thông tin nhất định để tìm nạp báo cáo. Tôi muốn chuyển hướng đến một trang khác và chuyển một số thông tin về ngoại lệ, nhưng tôi không thể tìm ra cách thực hiện điều này trong MVC3 ....

Bất kỳ ý tưởng nào?

Trả lời

2

Thay vì cho phép thư viện phần thứ ba viết thư trả lời trực tiếp, hãy sử dụng thường xuyên ActionResult và trả lại File(...) cho tệp thực hoặc RedirectToAction(...) (hoặc RedirectToRoute(...)) về lỗi. Nếu thư viện bên thứ 3 của bạn chỉ có thể viết thư cho Response, bạn có thể cần phải sử dụng một số thủ thuật để nắm bắt đầu ra của nó.

[HttpGet] 
public ActionResult GetExcel(int id) 
{ 
    try 
    { 
    var report = _reportService.GetReport(id); 
    var table = _reportService.GetReportTable(id); 
    var excelReport = new ExcelReport(table, report.Name); 
    var content = excelReport.MakeReport(System.Web.HttpContext.Current.Response); 
    return File(content, "application/xls", "something.xls"); 
    } 
    catch (Exception ex) 
    { 
    RedirectToRoute("/Report/Error/", new { exceptionType = ex.GetType().Name }); 
    } 
} 
8

Bạn có thể làm một

Response.Redirect(Url.Action("Error", "Report", new { exceptionType = ex.GetType().Name }); 

Nhưng bạn đã thực hiện một cái nhìn tại các FilePathResult hoặc FileStreamResult?

+0

Cảm ơn lớn. Nó làm việc cho tôi. – Mohan

2

Bạn có thể trả về một EmptyActionResult:

[HttpGet] 
public ActionResult GetExcel(int id) 
{ 
     try 
     { 
     var report = _reportService.GetReport(id); 
     var table = _reportService.GetReportTable(id); 
     var excelReport = new ExcelReport(table, report.Name); 
     excelReport.DownloadReport(System.Web.HttpContext.Current.Response); 

     return new EmptyResult(); 
     } 
     catch (Exception ex) 
     { 
     return RedirectToAction("Error", "Report", rnew { exceptionType = ex.GetType().Name });  
     } 

} 

Không chắc chắn nếu nó hoạt động, đã không kiểm tra nó.

0

cách tiếp cận khác sẽ được sử dụng một bộ lọc ngoại lệ:

public class MyExceptionFilter : FilterAttribute, IExceptionFilter 
{ 
    public void OnException(ExceptionContext filterContext) 
    { 
     var routeValues = new RouteValueDictionary() 
     { 
      { "controller", "Error" }, 
      { "action", "Report" } 
     }; 

     filterContext.Result = new RedirectToRouteResult(routeValues); 
     filterContext.ExceptionHandled = true; 

     // Or I can skip the redirection and render a whole new view 

     //filterContext.Result = new ViewResult() 
     //{ 
     // ViewName = "Error" 
     // //.. 
     //}; 
    } 
} 
Các vấn đề liên quan