Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Useful info from @Darin Dimitrov that <code>HttpNotFoundResult</code> is actually returning empty result.</p> <p>After some study. The workaround for <strong>MVC 3</strong> here is to derive all <code>HttpNotFoundResult</code>, <code>HttpUnauthorizedResult</code>, <code>HttpStatusCodeResult</code> classes and implement <strong>new</strong> (overriding it) <code>HttpNotFound</code>() method in <code>BaseController</code>.</p> <p>It is best practise to use base Controller so you have 'control' over all derived Controllers.</p> <p>I create new <code>HttpStatusCodeResult</code> class, not to derive from <code>ActionResult</code> but from <code>ViewResult</code> to render the view or any <code>View</code> you want by specifying the <code>ViewName</code> property. I follow the original <code>HttpStatusCodeResult</code> to set the <code>HttpContext.Response.StatusCode</code> and <code>HttpContext.Response.StatusDescription</code> but then <code>base.ExecuteResult(context)</code> will render the suitable view because again I derive from <code>ViewResult</code>. Simple enough is it? Hope this will be implemented in the MVC core.</p> <p>See my <code>BaseController</code> bellow:</p> <pre><code>using System.Web; using System.Web.Mvc; namespace YourNamespace.Controllers { public class BaseController : Controller { public BaseController() { ViewBag.MetaDescription = Settings.metaDescription; ViewBag.MetaKeywords = Settings.metaKeywords; } protected new HttpNotFoundResult HttpNotFound(string statusDescription = null) { return new HttpNotFoundResult(statusDescription); } protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null) { return new HttpUnauthorizedResult(statusDescription); } protected class HttpNotFoundResult : HttpStatusCodeResult { public HttpNotFoundResult() : this(null) { } public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { } } protected class HttpUnauthorizedResult : HttpStatusCodeResult { public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { } } protected class HttpStatusCodeResult : ViewResult { public int StatusCode { get; private set; } public string StatusDescription { get; private set; } public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { } public HttpStatusCodeResult(int statusCode, string statusDescription) { this.StatusCode = statusCode; this.StatusDescription = statusDescription; } public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } context.HttpContext.Response.StatusCode = this.StatusCode; if (this.StatusDescription != null) { context.HttpContext.Response.StatusDescription = this.StatusDescription; } // 1. Uncomment this to use the existing Error.ascx / Error.cshtml to view as an error or // 2. Uncomment this and change to any custom view and set the name here or simply // 3. (Recommended) Let it commented and the ViewName will be the current controller view action and on your view (or layout view even better) show the @ViewBag.Message to produce an inline message that tell the Not Found or Unauthorized //this.ViewName = "Error"; this.ViewBag.Message = context.HttpContext.Response.StatusDescription; base.ExecuteResult(context); } } } } </code></pre> <p>To use in your action like this:</p> <pre><code>public ActionResult Index() { // Some processing if (...) return HttpNotFound(); // Other processing } </code></pre> <p>And in _Layout.cshtml (like master page)</p> <pre><code>&lt;div class="content"&gt; @if (ViewBag.Message != null) { &lt;div class="inlineMsg"&gt;&lt;p&gt;@ViewBag.Message&lt;/p&gt;&lt;/div&gt; } @RenderBody() &lt;/div&gt; </code></pre> <p>Additionally you can use a custom view like <code>Error.shtml</code> or create new <code>NotFound.cshtml</code> like I commented in the code and you may define a view model for the status description and other explanations.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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