2012-10-17 31 views
5

Tôi đang cố tải URL đầy đủ nhưng RouteUrl đang trả về trống.Url.RouteUrl trả lại trống

Trong Xem, Tôi gọi như thế:

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

Đây là tuyến cấu hình của tôi:

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 } 
    ); 
} 

và bộ điều khiển của tôi:

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); 
    } 
} 

Trả lời

10

Thêm httproute = "" đến routeValues:

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

Ok nó hoạt động. tks rất nhiều, nhưng tôi không hiểu tại sao tôi cần tham số này? – will

+0

Bạn cần tham số này để chỉ báo cho công cụ định tuyến mà bạn cần tạo một tuyến API và không phải là tuyến đường MVC tiêu chuẩn. –

+0

humm ... ok..tks. – will

Các vấn đề liên quan