开发者

Run MVC controller action without the view?

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:

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

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.

Before

public ActionResult GenerateExcel()
{
    return this.Excel(parameters);
}

After

    public void GenerateExcel()
{
ExcelResult excelResult = this.Excel(parameter开发者_开发百科s);
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.


Make sure your action method does not return a ActionResult:

public void DoSomething()


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜