.net - Routing based on requested datatype in ASP.NET MVC -
I am trying to write REST behavior in my ASP.NET MVC2 app, but I have to spend a hard time knowing me That's how the routes work.
I would like my routing to work like this:
/ user / go / 1 < - Provides regular HTML-based answers / users / gate.xml / 1 & lt; - Data obtained from Get XML /Users/Get.json/1 - & lt; - Data as JSON
I have tried to set routes like this:
routes. MapRoute ("rest", "{administrator} / {action} {format} / {id}" (...)
but complains that I {action} and { Format}
need a separator between:
routes.MapRoute ("left", "{controller} / {action}. {Format} / {id } "(...)
creates / user / receives / 1 invalid (needs to do this
any suggestions?
------------- Edit it ------- ---------------------------- -
I have a solution now, but I'm not really happy with it:
Routes. MapRoute ("DefaultWithFormat", // root no New {Admin = "Home", Action = "Index", Format = "HTML", ID = URL Parameter, with "URL" {{Admin} / {action}. {Format} / {id} "// URL Parameter. Option)} parameter;) New with the URLRP ("default", // root name "{controller} / {action} / {id}", // URL parameter {admin = "home", action = " Index ", id = UrlParameter.Optional} / / parameter defaults);
Both the / user / gate. Whatever format / 1 works with / user / gate / 1
The reason for this is that when I get the bus / user / / 1 (without format) it will leave the first path , And goes on to the next, in which the format is not included, I have created an ActionFilterAttribute to handle the return and override the OnActionExecuted method like this:
var type = filterContext RouteData.Values ["format"]; If (type! = Null & amp; attribute! = Null) {if (type == "html") returns; If (type.Toostring (). ToLower () == "xml" and & amp; attributes.Any (a => A.Loward type Any (a2 = & gt; a2 == ResponseType.xml)) ) {FilterContext result = new XmlResult (filterContext.Controller.ViewData.Model); FilterContext.HttpContext.Response.Clear (); FilterContext.HttpContext.Response.ContentType = "text / xml"; Return; } (If.ToString (.) ToLower () == "Jason" and & amp; Attributes. Any (A => A.Elward Type Any Any (a2 = & gt; a2 == ResponseType.JSON) )) {FilterContext .Result = new JsonResult () {data = (filterContext.Controller.ViewData.Model), JsonRequestBehavior = JsonRequestBehavior.AllowGet}; FilterContext.HttpContext.Response.Clear (); FilterContext.HttpContext.Response.ContentType = "text / json"; Return; }}
And I also have a ResponseTypeAttribute which allows me to decorate with all the tasks that they should allow to return:
[AttributeUsage (AttributeTargets.All, denied = false, allow = multilevel)] Public seal class ResponseType attribute: attribute {list & lt; Reaction Type & gt; AllowedTypes; Public listing & lt; ResponseType & gt; Allowed type {received {return allowedTypes; } Set {allowedTypes = value; }} Public reactive typing attribute (parameter response type [] permitted type) {this.allowedTypes = new list & lt; ResponseType & gt; (); This.allowedTypes.AddRange (allowedTypes); }} Public enum ResponseType {XML, JSON}
XmlResult is just a simple object serializer.
Brad Wilson talked to an advanced ASP.NET MVC2 with the title, where he has an example It's shown what you really want to do. You can download slides and example codes here:
(This is the first thing on the page, and if I miss the URL is a good thing then the first topic in the matter is .)
Comments
Post a Comment