如何从asp.net MVC 4应用程序中的FileResult方法返回错误消息?

时间:2022-10-21 04:13:50

I have a fileresult method in asp.net mvc4 that returns a report in an excel file. Now how can i return an error message from this method if my conditions are not met !! Since we can only return a file from this method ?! Thnks

我在asp.net mvc4中有一个fileresult方法,它在excel文件中返回一个报告。现在,如果我的条件不满足,我怎么能从这个方法返回错误信息!!既然我们只能从这个方法返回一个文件?! Thnks

1 个解决方案

#1


12  

You can change signature of action method to public ActionResult MyMethod() and return FileResult when ModelState.IsValid==true and ViewResult when ModelState.IsValid==false

您可以将action方法的签名更改为公共ActionResult MyMethod(),并在ModelState.IsValid == true时返回FileResult,在ModelState.IsValid == false时返回ViewResult

#1


12  

You can change signature of action method to public ActionResult MyMethod() and return FileResult when ModelState.IsValid==true and ViewResult when ModelState.IsValid==false

您可以将action方法的签名更改为公共ActionResult MyMethod(),并在ModelState.IsValid == true时返回FileResult,在ModelState.IsValid == false时返回ViewResult