2012-10-17 8 views
5

Próbuję uzyskać pełny adres URL, ale RouteUrl powraca puste.Url.RouteUrl wracający pusty

w widoku, jestem nazywając tak:

alert('@Url.RouteUrl("Api", new { controller = "Parametros", id = "" })'); 

Oto moje szlaki konfiguracje:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

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

    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Usuario", 
      action = "Login", id = UrlParameter.Optional } 
    ); 
} 

i mój kontroler:

public class ParametrosController : ApiController 
{ 
    ISistemaService _sistemaService; 
    public ParametrosController(Empresa empresa, ISistemaService sistemaService) 
    { 
     _sistemaService = sistemaService; 
    } 


    public PagedDataModel<ParametroDTO> Get(
     [FromUri]ParametroFiltro filter, int page, int pageSize) 
    { 
     int total = 0; 
     var list = _sistemaService.Paging(filter, page, pageSize, out total); 
     return new PagedDataModel<ParametroDTO>(page, pageSize, total, list); 
    } 

    public ParametroDTO Get(string codigo) 
    { 
     return _sistemaService.GetParametroPorCodigo(codigo); 
    } 
} 

Odpowiedz

10

Dodaj do httproute = ""routeValues:

alert('@Url.RouteUrl("Api", 
    new { httproute = "", controller = "Parametros", id = "" })'); 
+0

OK, zadziałało. tks dużo, ale nie rozumiem, dlaczego potrzebuję tego parametru? – will

+0

Potrzebujesz tego parametru, aby wskazać silnikowi routingu, który chcesz wygenerować trasę API, a nie standardową trasę MVC. –

+0

humm ... ok..tks. – will