MVC4

2012-08-01 8 views
1

내가 방금 만든 개체에 URL 얻기 위해 노력하고 있어요에서 개체를 생성 한 후 절대 URL을 얻는 방법 : 국도MVC4

routes.MapHttpRoute(
    name: "ApiRoute", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 

여기

code = HttpStatusCode.Created; 
String location = Url.Route("ApiRoute", new {@id = bp.Id, @controller = "ProfileController" }); 
Response.AppendHeader("Location", location); 

을하지만 보인다 MVC3에서 MVC4로 변경되었습니다.

누군가는이 되거 수 있습니까?

감사

답변

1

당신은 매개 변수를 받아들이는 Url.RouteUrl() 방법을 사용할 수 있습니다. 이렇게하면 정규화 된 URL이 반환됩니다.

// Formatted for readability 
code = HttpStatusCode.Created; 
String location = Url.Route(
    "ApiRoute", 
    new {@id = bp.Id, @controller = "ProfileController" }, 
    "http" 
); 
Response.AppendHeader("Location", location); 
+0

좋아. 따라서 귀하가 제공 한 예는하지만, 여전히 경로를 보여줍니다. 그러면 RouteUrl이어야합니다? 고마워! :) –

+0

난 그냥 그것을 밖으로 시도하고 Url.RouteUrl 당신이 볼 수있는 정보가 널 (null)을 반환합니다. 또한 루트를 원래 게시물에 추가했습니다. –