麻烦搞清楚routes.Maproute此URL(C#)

编程入门 行业动态 更新时间:2024-10-25 20:25:36
本文介绍了麻烦搞清楚routes.Maproute此URL(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尽管URL路由的帖子多如牛毛,我还没有就这一问题达成启示。

我有一个MVC应用程序工作正常,但我想获得幻想,并做一些URL重写,使其更容易在我的用户。

缺省路由是平时的一个微小的变化:

routes.MapRoute(      默认,      {控制器} / {行动} / {查询},      新{控制器=SomeController,行动=索引,查询=});

我想要做的是重新路由的网址 mysite/12345 到SomeController的详细行动12345查询。我想这样的:

routes.MapRoute(      DirectToDetails      {查询},      新{控制器=SomeController,行动=详细信息查询=});routes.MapRoute(      默认,      {控制器} / {行动} / {查询},      新{控制器=SomeController,行动=索引,查询=});

但最终掩盖 mysite/ 以及它去SomeController /细节,而不是SomeController的/索引。

编辑:

@乔恩每次的意见,我删除了查询=的匿名对象:

routes.MapRoute(      DirectToDetails      {查询},      新{控制器=SomeController,行动=详细信息});

...其中涉及的问题尽可能去;现在,当我在查看/ SomeView /索引提交表单(即提交上述SomeController /详细信息)我得到一个资源无法找到的错误。

下面的形式声明:

<%使用(Html.BeginForm(              细节,              SomeController              FormMethod.Post,              新{ID =SearchForm})){%GT;    <%Html.RenderPartial(SearchForm模型); %GT;    <%= Html.ValidationMessageFor(型号=> model.Query)%GT;<%}%GT;

解决方案

删除查询=(显然这不是可选正是因为这个问题你遇到 - 没必要把它的第三个参数,如果没有合理的缺省值),并使用图路线的重载需要4个参数与路由约束:

routes.MapRoute(  DirectToDetails  {查询},  新{控制器=SomeController,行动=详细信息},  新{查询= @\\ D +});

显然,你将需要调整正则表达式,如果它不是一个整数...

在回答您的编辑...确保你已经删除从短路线查询默认的 - 你已经发布的默认一个人也没有。也建议你仔细检查的形式,并确保它张贴值查询(也许设置岗位操作方法一个破发点,并确保查询参数被正确绑定?)。

Despite the plethora of URL routing posts, I have yet to reach enlightenment on the subject.

I have an MVC app that works fine, but I want to get fancy and do some URL rewriting to make it easier on my users.

The default route is a slight variation on the usual:

routes.MapRoute( "Default", "{controller}/{action}/{query}", new{ controller = "SomeController", action="Index", query = "" });

What I want to do is re-route the URL mysite/12345 to the Details action of SomeController with the 12345 query. I tried this:

routes.MapRoute( "DirectToDetails", "{query}", new{ controller = "SomeController", action="Details" query="" }); routes.MapRoute( "Default", "{controller}/{action}/{query}", new{ controller = "SomeController", action="Index", query = "" });

but that ends up obscuring mysite/ and it goes to SomeController/Details instead of SomeController/Index.

EDIT:

per the advice of @Jon I removed the query = "" in the anonymous object:

routes.MapRoute( "DirectToDetails", "{query}", new{ controller = "SomeController", action="Details" });

... which addressed the question as far as it went; now when I submit the form at Views/SomeView/Index (that submits to the aforementioned SomeController/Details) I get a "resource cannot be found" error.

Here is the form declaration:

<% using (Html.BeginForm( "Details", "SomeController", FormMethod.Post, new { id = "SearchForm" })) { %> <% Html.RenderPartial("SearchForm", Model); %> <%= Html.ValidationMessageFor(model => model.Query) %> <% } %>

解决方案

Remove the query = "" (obviously it's not optional exactly because of the problem you're having - no need to put it in the 3rd argument if there's no sensible default) and use the overload of MapRoute that takes a 4th parameter with route constraints:

routes.MapRoute( "DirectToDetails", "{query}", new { controller = "SomeController", action="Details" }, new { query = @"\d+" });

Obviously you will need to adjust the regex if it's not an integer...

In response to your edit...make sure that you've removed the default for query from the "short" route - you've posted the default one there. Also suggest you double check the form and make sure it's posting a value for query (maybe set a break point on the post action method and make sure the query parameter is being bound correctly?).

更多推荐

麻烦搞清楚routes.Maproute此URL(C#)

本文发布于:2023-06-07 15:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:麻烦   routes   URL   Maproute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!