Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are Controller methods like RedirectToAction() and HttpNotFound() protected internal?
    text
    copied!<p>I want to implement a service pattern to provide some logic for several of my ASP.NET MVC controllers. Depending on the result of the logic, I may want the controllers to redirect, or give a 404 not found, or various other responses. Therefore I want my controllers to be able to receive an <code>out ActionResult</code> parameter from the service method(s) that they can return to carry out these responses.</p> <p>The trouble is that I don't see any way for the service to generate <code>ActionResult</code>s. Because methods like <code>RedirectToAction()</code> and <code>HttpNotFound()</code> in the <code>Controller</code> class are set to <code>protected internal</code>, they cannot be accessed from the service class.</p> <p>What is the rationale behind making them <code>protected internal</code>? How is my service supposed to indicate to the controllers what action result they should return?</p> <p>An example of what I might want to do is allow the user, in several places, to set a configuration variable if they have permission. The controller code would look something like:</p> <pre><code>public ActionResult SetVariable(int variableId, int variableValue) { ActionResult actionRes; _svcVars.SetVariable(variableId, variableValue, out actionRes); return actionRes; } </code></pre> <p>... and the service code would look something like:</p> <pre><code>public bool SetVariable(int variableId, int variableValue, out ActionResult actionRes) { ConfigRepository repoConfig = new ConfigRepository(); if (!repoConfig.VariableExists(variableId)) { actionRes = HttpNotFound("Could not find config variable"); return false; } if (!repoConfig.VariableMayBeChanged(variableId)) { actionRes = Redirect("/ChangeForbidden.html"); return false; } int variablesListId = repoConfig.SetVariable(variableId, variableValue); actionRes = RedirectToAction("ViewVariablesList", new { listId = variablesListId }); return true; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload