如何在 MVC3 中生成自定义 URL,而不是将我的参数作为查询字符串传递

发布于 2025-01-04 16:03:28 字数 838 浏览 2 评论 0原文

当我输入 ../Search/Recipes/Deserts/Pie 这样的 URL 时,我可以重定向到该页面(即在 SearchController 中,操作方法将是 Recipes ,参数将是 Deserts & Pie 。

注意:要实现相同的效果我在 Global.asax 中添加了一条路由,如下所示:

    routes.MapRoute(
            "SearchRecipes",                                              
            "Search/Recipes/{category}/{type}",                           
            new { controller = "Search", action = "Recipes", category = "all" , type = ""}  
 );

但是如何通过我们的代码生成该 URL?当我尝试在 Url.Action 中添加路由值时,它会作为查询字符串出现

。例如,

Url.Action( "Recipes" , "Search" , new { type = item.type, isPie = item.isPie} )

会给我看起来像这样的链接: Search/Recipes ?type=Deserts&isPie=Y

但我想生成像 Search/Recipes/Deserts/Y 这样的 url

。 基本上我不想

将参数作为查询字符串传递,但想遵守 \ 分隔的值。

When I type the URL like ../Search/Recipes/Deserts/Pie, I can get redirected to that page (i.e in SearchController, action method would be Recipes & parameters would be Deserts & Pie .

NB: To achieve the same I ve added a route in my Global.asax, that looks like :

    routes.MapRoute(
            "SearchRecipes",                                              
            "Search/Recipes/{category}/{type}",                           
            new { controller = "Search", action = "Recipes", category = "all" , type = ""}  
 );

But how to generate that URL through our code? When I try to add a route value in Url.Action, it comes as a query string.

For example,

Url.Action( "Recipes" , "Search" , new { type = item.type, isPie = item.isPie} )

would give me link that looks like : Search/Recipes?type=Deserts&isPie=Y,

but I want to generate url like Search/Recipes/Deserts/Y.

Please let me know how we can achieve the same?

Basically I dont want to pass my parameters as query string, but want to adhere to \ separated values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨离汐 2025-01-11 16:03:28

尝试

 routes.MapRoute(
        "SearchRecipes",                                              
        "Search/Recipes/{category}/{type}/{isPie}",                           
        new { controller = "Search", action = "Recipes", category = "all" , type = "", isPie="N"}  
  );
@Url.RouteUrl("SearchRecipes",new{category=item.Category,type=item.type,isPie=itme.isPie})

定义路由时未指定的任何查询参数都将添加为 ?param=value

Try

 routes.MapRoute(
        "SearchRecipes",                                              
        "Search/Recipes/{category}/{type}/{isPie}",                           
        new { controller = "Search", action = "Recipes", category = "all" , type = "", isPie="N"}  
  );
@Url.RouteUrl("SearchRecipes",new{category=item.Category,type=item.type,isPie=itme.isPie})

Any query parameter not specified when defining a route will be added as ?param=value

俯瞰星空 2025-01-11 16:03:28

您可以使用 UrlHelper.RouteUrl 方法

Url.RouteUrl("SearchRecipes", new {category = item.category, type=item.type});

You can use the UrlHelper.RouteUrl method

Url.RouteUrl("SearchRecipes", new {category = item.category, type=item.type});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文