Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(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.

like image 515
DougJones Avatar asked Jan 13 '11 19:01

DougJones


People also ask

How do I call a void controller without leaving the view?

Basically @Html. ActionLink() or <a></a> tag uses get request to locate the page. Hence whenever you clicked it, you request to your AddToCart action method in ProductController and if that action method returns null or void so a blank or empty page is shown as you experienced (because or @Html.

Can action method be private?

All the public methods of the Controller class are called Action methods. They are like any other normal methods with the following restrictions: Action method must be public. It cannot be private or protected.

Can we override action method in MVC?

For MVC action command overrides, extend the BaseMVCActionCommand class, and the only method you'll need to override is doProcessAction , which must return void . It's straightforward to override MVC action commands while keeping your code decoupled from the original action methods.


2 Answers

Make sure your action method does not return a ActionResult:

public void DoSomething()
like image 110
Ufuk Hacıoğulları Avatar answered Sep 19 '22 09:09

Ufuk Hacıoğulları


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.

enter image description here

I believe,this answers your question.

like image 43
Sharad Avatar answered Sep 18 '22 09:09

Sharad