ASP.NET MVC2 앱에 REST 비헤이비어를 작성하려고하지만 원하는대로 해당 경로를 작동시키는 방법을 찾지 못했습니다.ASP.NET MVC에서 요청한 데이터 유형을 기반으로하는 라우팅
/Users/Get/1 <- returns a regular HTML-based reply
/Users/Get.xml/1 <- returns the data from Get as XML
/Users/Get.json/1 <- returns the data as JSon
나는이 같은 경로를 설정하려고했습니다 :
내 라우팅을하고 싶습니다은 다음과 같이 작동합니다
routes.MapRoute("Rest",
"{controller}/{action}{format}/{id}" (...)
을하지만 내가 사이에 구분이 필요 불만 {동작} 및 {형식}도
다음
routes.MapRoute("Rest",
"{controller}/{action}.{format}/{id}" (...)
은/Users/Get/1을 유효하지 않게 만듭니다 (/Users/Get./1은 받아 들일 수 없습니다)
어떤 제안이 있습니까?
------------- EDIT -------------------------------- ----
내가 지금 하나 개의 솔루션을 가지고,하지만 난 그것으로 정말 행복하지 않다 :
이것은 또한 /Users/Get.whateverFormat/1와 모두 작동routes.MapRoute(
"DefaultWithFormat", // Route name
"{controller}/{action}.{format}/{id}", // URL with parameters
new { controller = "Home", action = "Index", format = "HTML", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
/사용자/받기/1
이유는 내가/사용자/가져 오기/1 (.format없이)하면 첫 번째 경로를 건너 뛴 다음 형식을 포함하지 않는 다음으로 이동한다는 것입니다. 내가 ActionFilterAttribute 만든 반환을 처리하고이 같은 OnActionExecuted 방법을 재정의하려면 :
var type = filterContext.RouteData.Values["format"];
if (type != null && attributes != null)
{
if (type == "HTML") return;
if (type.ToString().ToLower() == "xml" && attributes.Any(a => a.AllowedTypes.Any(a2 => a2 == ResponseType.XML)))
{
filterContext.Result = new XmlResult(filterContext.Controller.ViewData.Model);
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.ContentType = "text/xml";
return;
}
if (type.ToString().ToLower() == "json" && attributes.Any(a => a.AllowedTypes.Any(a2 => 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;
}
}
을 그리고 나는 또한 그들이 허용해야 returnType이 무엇인지와 행동 장식 나를 수있는 ResponseTypeAttribute 있습니다
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class ResponseTypeAttribute : Attribute
{
List<ResponseType> allowedTypes;
public List<ResponseType> AllowedTypes
{
get { return allowedTypes; }
set { allowedTypes = value; }
}
public ResponseTypeAttribute(params ResponseType[] allowedTypes)
{
this.allowedTypes = new List<ResponseType>();
this.allowedTypes.AddRange(allowedTypes);
}
}
public enum ResponseType
{
XML, JSON
}
을
XmlResult는 단순한 개체 serializer입니다.
슬라이드가 다음과 같이 말하기 때문에 실제로 도움이되지 않습니다. "데모 - REST와 유사한 URL 지원"예제 코드에는 예제가 없습니다. –
이 비디오를 보았습니다 : http : //events.boostweb20 .com/Events/SeattleCodeCamp2010/# state = sessionCode % 242003-4. 시간이 있으면 봐. :) 그것은 편안한 URL에 대한 데모를 보여줍니다 .... – apolka
이제는 꽤 달콤한 해결책입니다! : D –