没有视图运行MVC控制器操作?

时间:2022-12-01 23:20:56

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?

没有关联的View,因此没有文件,但不应该有。我究竟做错了什么?

UPDATE

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身份验证存在安全问题,但是他们“离开了”(意思是我希望他们回来)。但是现在,一切都运转正常。

3 个解决方案

#1


15  

Make sure your action method does not return a ActionResult:

确保您的操作方法不返回ActionResult:

public void DoSomething()

#2


5  

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(),该方法在我们尝试在不存在的控制器上调用操作(或者我们请求没有匹配操作方法的视图时)执行。

没有视图运行MVC控制器操作?

I believe,this answers your question.

我相信,这回答了你的问题。

#3


0  

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模型。

#1


15  

Make sure your action method does not return a ActionResult:

确保您的操作方法不返回ActionResult:

public void DoSomething()

#2


5  

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(),该方法在我们尝试在不存在的控制器上调用操作(或者我们请求没有匹配操作方法的视图时)执行。

没有视图运行MVC控制器操作?

I believe,this answers your question.

我相信,这回答了你的问题。

#3


0  

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模型。