0

我有一个CodesController,Index 操作看起来像这样。

public ActionResult Index(int id, string manufacturerName)

我希望 URL 看起来像http://www.domain.com/codes/[id]/[manufacturerName]但我目前使用的代码看起来像http://www.domain.com/代码/[id]?manufacturerName=[manufacturerName]

<script type="text/javascript">
    $(function() {
        $('#btnNext').click(function () {
            var _id = $('#ddlManufacturers').val();
            var _manufacturerName = $('#ddlManufacturers').text();
            var url = '@Url.Action("Index", "Codes", new { id = "_id", manufacturerName = "_manufacturerName" })'
            .replace('_id', _id).replace('_manufacturerName', _manufacturerName.toLowerCase().trim());
            window.location.href = url;
        });
    });

这就是我现在的做法,不知道如何正确地做到这一点,所以 URL 变成了我想要的方式。编码

4

2 回答 2

1

根据我的理解,您需要添加一条路线,例如

routes.MapRoute(
    name: "CodesRoute",
    url: "{controller}/{action}/{id}/{manufacturerName}",
    new { controller = "Codes",  action = "Index",
        manufacturerName = UrlParameter.Optional 
    }
);

之后,您可以使用Html.RouteLinkUrl.RouteUrl

当你使用它时,JavaScript Url.RouteUrl会很有用

var url = '@Url.RouteUrl("CodesRoute", new { id = "_id", manufacturerName = "_manufacturerName" })'
于 2013-10-02T19:22:23.063 回答
0

您将需要一条新路线:

routes.MapRoute(
    name: null,
    url: "{controller}/{action}/{id}/{manufacturerName}",
    defaults: new { 
        controller = "Home", 
        action = "Index", 
        id = UrlParameter.Optional, 
        manufacturerName = UrlParameter.Optional 
    }
);
于 2013-10-02T19:20:23.650 回答