0

MVC 3を使用していますが、問題があります。私が期待したようにmydomain/mydirectory / itemを与える代わりに、私はこれを手に入れます:

mydomain / mydirectory / list?animal=quack。

これがグローバルなルートです

//Default route mapping
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { controller = @"[^\.]*", action = @"[^\.]*" }
);

リンクを構築する方法を示すコード:

   <div id="main-content" title="AnimalBox" style="float:none;">
        <% Html.DataList(Model.PriceListAnimals).Columns(7).Item(item =>
                {
                    item.Template(galleryImage =>  
                    {%>
                        <div style="margin-left:20px; line-height:150%;">
                            <span><%= Html.ActionLink(galleryImage.AnimalName,"List",new { @animal = galleryImage.AnimalName }) %></span>
                        </div>  
                        <%  });
                }).Render(); %>
    </div>

何か案は?

4

2 に答える 2

0

You have to define a route for special case routing like your instance. Since you are passing 'animal' as a parameter, you should create a route to handle that instance. In your global.aspx (above the default route), create something like below:

routes.MapRoute(
    "Animal", // A distinct Route name
    "{controller}/{action}/{animal}", // URL with parameters
    new { controller = "MyDirectory", action = "List"}
);

This will define a route to the MyDirectory controller on the action list which has an animal parameter which is NOT OPTIONAL. Defining routes is what enables you to generate clean URLs from the html helpers and other methods (redirect to action, etc).

于 2012-07-25T04:38:39.830 に答える
0

Html.ActionLink のオーバーロードは次のとおりです。

Html.ActionLink("linkText", "actionName", "controller", object routeValues, object HtmlAttributes)

おっしゃる通り、mydirectory=コントローラ、List=アクションですね。その場合は、次を試してください。

<%= Html.ActionLink(galleryImage.AnimalName, "List", "mydirectory", new { @id = galleryImage.AnimalName }, null) %>

これにより、次が生成されます。

<a href="/mydirectory/list/quack">quack</a>
于 2012-07-25T02:09:12.673 に答える