Ist es möglich, ASP.NET MVC 3-Routenschränkungen so zu ändern, dass sie 400 schlechte Anfrage mit JSON-Körpern zurückgeben?

StackOverflow https://stackoverflow.com/questions/9504001

Frage

Ich entwickle einige Ruhestellungen mit der ASP.NET MVC 3-Plattform. Bisher war es großartig, ich liebe die Flexibilität von MVC 3, und es ist eine Brise, mit ihm Ruhedienste zu erstellen. Ein Bereich, in dem ich eine schwierige Zeit hatte, sind die Routenschränkungen in Global.ASAX. Zum einen scheinen sie nie ordnungsgemäß zu funktionieren (einschließlich eines, der immer 404 zurückgibt, auch wenn der Eingang den Anforderungen der Einschränkung definitiv erfüllt ... aber das ist eine andere Frage).

Sekunden, und noch wichtiger ist das Ergebnis, in dem eine Einschränkung fehlschlägt, ist immer eine HTML-Seite. Angenommen, die Einschränkungen arbeiten, haben HTML einen Schraubenschlüssel in die Mischung, wenn alle Verbraucher dieser REST-Dienste Datentypen akzeptieren, z. B. Anwendung / JSON, Text / XML, möglicherweise BSON usw. Ich brauche wirklich Um Fehler in unseren Kunden direkt adressieren zu können, anstatt einfach aufzublasen und den HTML für die Auflösung von Seitenband aufzurufen.

Kann man ändern, was als Antwort auf eine Routenbeschränkung zurückgegeben wird? Ist es möglich, die Dynamik zurückzugeben, so dass, wenn der Client, der die Anforderung ausgibt, nur Anwendung / BSO annimmt, eine akzeptable Antwort generieren, anstatt einfach eine Dosenreaktion eines einzelnen MIME-Typs zu erstellen?

War es hilfreich?

Lösung

About returning a code error instead of going to a controller you have to implemet a custom RouteHandler. This link 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.

However it is nice to do this in a controller filter...!

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

The code is here(It contains some controls on the "hint" provided by the client to prevent attacks by malicious users):

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) && hint.Length<=100 && 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);
    }
}

Andere Tipps

Errlusion might be of some help here.

You may be able to write some custom logic to check the HTTP Accept header and/or use [SomeContext].Request.IsAjaxRequest() to see if JSON or BSON would be appropriate to return -- or just return JSON or BSON for all types of requests if you prefer.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top