我正在尝试获取完整的URL,但是RouteUrl返回空。

在 View 中,我这样打电话:

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

和我的 Controller :
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);
    }
}

最佳答案

httproute = ""添加到routeValues中:

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

关于asp.net-mvc-3 - Url.RouteUrl返回空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12935172/

10-12 12:30