3

I have a webpage doing a redirection with two URLs parameters: id and bidId and I also have an other webpage with a redirection with two other URLs parameters: id and templateId.

I want to create the route to get a well formed url like those: localhost/12/50 but I have a problem with my routes.

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

When I am to the SubmitBid page the URL work perfectly but if I go to template page I have a url like this: localhost/5/?templateId=0

I don't understand why it's doesn't work and I need your help to understand why it is doing that. Thank you for your help. Karine


Edit: The way I am navigating is like this:

@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>
}
4

2 回答 2

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 
                });
于 2013-10-29T20:24:25.633 回答
1

我认为这是你的问题:

1)首先,您设置的第二条路线无关紧要。它永远不会被击中,也不会被用来建立链接。

2)其次,因此,当您在模板操作链接templateId的路由值中包含时,

 new { Id = "0", templateId = item.Id })

ASP.Net MVC 查看了您的路由,找到了第一个满足 NUMBER 个参数的路由,然后尝试使用链接上的路由值解析可选参数。由于 ASP.Net MVC 的要求已满足第一个路由,即 SubmitBid 路由,因此它找不到名称为 的路由值的可选参数templateId。它只有该路线的“Id”和“bidId”。此时 ASP.Net MVC 决定将其作为查询字符串粘贴在 url 上。(这也解释了为什么“ID”,即 url 中的 5 值,看起来是正确的。它是该路由上的一个可选参数。)

如果您在网页上执行“查看源代码”,您将看到链接是使用该查询字符串构建的。

以下是您可以解决此问题的方法:

由于您的第二条路线将永远不会被使用,请将其删除。更改您的第一条路线以使用更通用的第二个可选参数:

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

然后,您的BeginForm声明和ActionLink定义都将allPurposePrettyId在各自的路由值中使用。

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

@using (Html.BeginForm("SubmitBid", "Tender", 
new { id = Model.Id, allPurposePrettyId = "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>
}

显然,你应该得到一个更好的名字,allPurposePrettyId但你明白了它的要点。请记住,路由上设置的可选参数的名称也是表单和链接转到的操作方法的签名所期望的。

于 2013-10-25T05:42:23.677 回答