Url.RouteUrl返回空

时间:2012-10-17 13:15:09

标签: asp.net-mvc-3 asp.net-mvc-4

我正在尝试获取完整的网址,但RouteUrl将返回空白。

在视图中,我这样称呼:

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

这是我的路线配置:

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

和我的控制员:

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

1 个答案:

答案 0 :(得分:10)

httproute = ""添加到routeValues

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