具有多个参数MVC4的多个路由

Kar*_*ine 3 c# asp.net-mvc routes

我有一个网页使用两个URL参数进行重定向:idbidId我还有一个其他网页,其中包含两个其他URL参数的重定向:idtemplateId.

我想创建一个路由来获得一个格式良好的URL,如:localhost/12/50,但我的路线有问题.

            routes.MapRoute(
        name: "SubmitBid",
        url: "{controller}/{action}/{id}/{bidId}/",
        defaults: new { controller = "Tender", action = "SubmitBid", id = UrlParameter.Optional, bidId = UrlParameter.Optional });

        routes.MapRoute(
        name: "Tender",
        url: "{controller}/{action}/{id}/{templateId}",
        defaults: new { controller = "Tender", action = "Create", id = UrlParameter.Optional, templateId = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)

当我进入SubmitBid页面时,URL工作正常,但如果我去模板页面,我有一个这样的URL:localhost/5 /?templateId = 0

我不明白为什么它不起作用,我需要你的帮助才能理解它为什么这样做.谢谢您的帮助.卡林纳


编辑:我导航的方式是这样的:

@Html.ActionLink(this.LocalResources("Use"), VIA.Enums.ActionName.UseTemplate.GetStringValue(), new { Id = "0", templateId = item.Id })

@using (Html.BeginForm("SubmitBid", "Tender", new { id = Model.Id, bidId = "0" }, FormMethod.Get, null))
{
    <div style="text-align: center; margin: 20px 0 0 0">
        <button class="btn btn-large btn-primary" type="submit">@this.LocalResources("Bid.Text")</button>
    </div>
}
Run Code Online (Sandbox Code Playgroud)

Hen*_*ema 6

您应该为两个控制器操作添加特定路由:

routes.MapRoute(
    name: "SubmitBid",
    url: "Tender/SubmitBid/{id}/{bidId}/",
    defaults: new 
                { 
                    controller = "Tender", 
                    action = "SubmitBid", 
                    id = UrlParameter.Optional, 
                    bidId = UrlParameter.Optional 
                });

routes.MapRoute(
    name: "Tender",
    url: "Tender/Create/{id}/{templateId}",
    defaults: new 
                { 
                    controller = "Tender", 
                    action = "Create", 
                    id = UrlParameter.Optional, 
                    templateId = UrlParameter.Optional 
                });

// Default route, keep this at last for fall-back.
routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new 
                { 
                    controller = "Home", 
                    action = "Index", 
                    id = UrlParameter.Optional 
                });
Run Code Online (Sandbox Code Playgroud)