asp.net-mvc 在没有视图的情况下运行 MVC 控制器操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4684332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run MVC controller action without the view?
提问by DougJones
I have an ExcelResult action result that returns Microsoft Excel documents, based off the Stephen Walther tip. Basically it just writes a stream out to the Response. When debugging VS 2010 (ASP.NET Dev Server), it runs fine, but when I run it on an IIS 6 box, I get the following error:
我有一个 ExcelResult 操作结果,它根据Stephen Walther 提示返回 Microsoft Excel 文档。基本上它只是将流写入响应。在调试 VS 2010 (ASP.NET Dev Server) 时,它运行良好,但是当我在 IIS 6 机器上运行它时,出现以下错误:
The view 'GenerateExcel' or its master was not found. The following locations were searched: ~/Views/Home/GenerateExcel.aspx ~/Views/Home/GenerateExcel.ascx ~/Views/Shared/GenerateExcel.aspx ~/Views/Shared/GenerateExcel.ascx
未找到视图“GenerateExcel”或其母版。搜索以下位置: ~/Views/Home/GenerateExcel.aspx ~/Views/Home/GenerateExcel.ascx ~/Views/Shared/GenerateExcel.aspx ~/Views/Shared/GenerateExcel.ascx
There is no associated View, and therefore no file, but there shouldn't have to be. What am I doing wrong?
没有关联的视图,因此没有文件,但不应该有。我究竟做错了什么?
UPDATE
更新
By simply returning void instead of an ActionResult, I no longer have this issue. Instead of returning the ExcelResult, I'm explicitly calling it's ExecuteResult method, which is writing to the output stream.
通过简单地返回 void 而不是 ActionResult,我不再有这个问题。我没有返回 ExcelResult,而是明确调用它的 ExecuteResult 方法,该方法正在写入输出流。
Before
前
public ActionResult GenerateExcel()
{
return this.Excel(parameters);
}
After
后
public void GenerateExcel()
{
ExcelResult excelResult = this.Excel(parameters);
excelResult.ExecuteResult(null);
}
After that, I had security issues with my NTLM authentication, but they 'went away' (meaning I expect them to come back). For now, though, everything is working properly.
在那之后,我的 NTLM 身份验证遇到了安全问题,但它们“消失了”(这意味着我希望它们会回来)。不过,就目前而言,一切正常。
回答by Ufuk Hac?o?ullar?
Make sure your action method does not return a ActionResult:
确保您的操作方法不返回 ActionResult:
public void DoSomething()
回答by Sharad
This is quite useful in a scenario when we have hundreds or thousands of views. Will in that case we create hundreds or thousands of controller actions? Of course not, then how can we fix it?
当我们有数百或数千个视图时,这非常有用。在这种情况下,我们会创建成百上千个控制器动作吗?当然不是,那我们怎么解决呢?
In the MVC Framework, the controller class includes a method, HandleUnknownAction(), that executes whenever we attempt to invoke an action (or when we request a view that has no matching action method) on a controller that does not exist.
在 MVC 框架中,控制器类包括一个方法 HandleUnknownAction(),每当我们尝试在不存在的控制器上调用操作(或当我们请求没有匹配操作方法的视图时),该方法就会执行。
I believe,this answers your question.
我相信,这回答了你的问题。
回答by Doug Lampe
I didn't look at the code for the action result in much detail, but there must be something wrong with your action result. Did you inherit from some other action result as opposed to the ActionResult class? Did you call base.ExecuteResult? If so, that would explain why it is looking for the view. I have created several custom controller actions to return various file types and they never look for a view.
动作结果的代码我没仔细看,但是你的动作结果肯定有问题。您是否继承了与 ActionResult 类相反的其他操作结果?你调用了 base.ExecuteResult 吗?如果是这样,那就可以解释它为什么要寻找视图。我创建了几个自定义控制器操作来返回各种文件类型,并且它们从不寻找视图。
I agree with the comments on the answer saying to return void. That definitely is a hack. You should not call ExecuteResult from inside your action. You are basically writing directly to the response stream from your controller action. Obviously it works but it really doesn't fit the MVC model.
我同意答案的评论说返回无效。这绝对是一个黑客。您不应该从您的操作内部调用 ExecuteResult。您基本上是从控制器操作直接写入响应流。显然它有效,但它确实不适合 MVC 模型。