Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>About returning a code error instead of going to a controller you have to implemet a custom RouteHandler. <a href="http://geekswithblogs.net/sankarsan/archive/2009/01/18/developing-custom-routehandler.aspx" rel="nofollow">This link</a> resumes all thing you can put the finger on ...and that you might modify this way. About "adapting" the return type...you can do this in the controller. It is enough to put som if and in some cases you return Json(...) and in other cases you return View or PartialView. </p> <p>However it is nice to do this in a controller filter...!</p> <p>I implemented a Control filter that allows the controller to negotiate the return type with the client. I is very simple...the client just declare the type that i would like to receive either in a route parameter (a piece of the url, or of the query string) or by posting a field that contains this info. The use is very simple...you rcontroller can return a View thai the "default" return type if no infos come from the client. Then the filter "change" automatically the result type before the View is invoked transformig it in what the client required. The possibilties handled by the filter are a view whose name is chosen by the client or Json</p> <p>The code is here(It contains some controls on the "hint" provided by the client to prevent attacks by malicious users):</p> <pre><code>public class AcceptViewHintAttribute : ActionFilterAttribute { private JsonRequestBehavior jsBehavior; public AcceptViewHintAttribute(JsonRequestBehavior jsBehavior = JsonRequestBehavior.DenyGet) { this.jsBehavior = jsBehavior; } public override void OnActionExecuted(ActionExecutedContext filterContext) { string hint = filterContext.RequestContext.HttpContext.Request.Params["ViewHint"]; if (hint == null) hint = filterContext.RequestContext.RouteData.Values["ViewHint"] as string; if (!string.IsNullOrWhiteSpace(hint) &amp;&amp; hint.Length&lt;=100 &amp;&amp; new Regex(@"^\w+$").IsMatch(hint) ) { ViewResultBase res = filterContext.Result as ViewResultBase; if (res != null) { if (hint == "json") { JsonResult jr = new JsonResult(); jr.Data = res.ViewData.Model; jr.JsonRequestBehavior = jsBehavior; filterContext.Result = jr; } else { res.ViewName = hint; } } } base.OnActionExecuted(filterContext); } } </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