ASP.NET MVC 路由两个参数之间有静态字符串

发布于 2024-08-17 20:51:41 字数 480 浏览 3 评论 0原文

这是我的路由:

routes.MapRoute(null,
    "shelves/{id1}/products/{action}/{id2}",
    new { controller = "Products", action = "List", id1 = "", id2 = ""});

想法是您可以执行以下操作:

http://server/shelves/23/products/edit/14

并且能够编辑货架 23 上的产品 14。使用 路由调试器,路径与路由匹配,但是当我尝试在路由调试器关闭的情况下导航到它时,它显示 HTTP 404 错误。有谁知道为什么会发生这种情况?

This is my routing:

routes.MapRoute(null,
    "shelves/{id1}/products/{action}/{id2}",
    new { controller = "Products", action = "List", id1 = "", id2 = ""});

The thought is that you can do something like this:

http://server/shelves/23/products/edit/14

And be able to edit product 14 on shelf 23. Checking it with Route Debugger, the path matches the routing, but when I try to navigate to it with Route Debugger off, it shows me a HTTP 404 error. Does anybody know why this is happening?

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

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

发布评论

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

评论(1

触ぅ动初心 2024-08-24 20:51:41

好吧,对于初学者来说, id1="" 行将会有问题,因为你不能让一些不在最后的可选内容。

我刚刚在我的系统上尝试过,效果很好。

这是路线:

routes.MapRoute(
    "shelf-route",
    "shelves/{id1}/products/{action}/{id2}",
    new { controller = "Products", action = "List", id2 = "" }
);

这是控制器:

public class ProductsController : Controller
{
    public string List(string id1, string id2)
    {
        return String.Format("ID1 = {0}, ID2 = {1}", id1, id2);
    }
}

我尝试了如下 URL:

http://localhost :14314/shelves/23/products/list/14
http://localhost:14314/shelves/23/products

他们工作得很好。

当您尝试使用其中包含“编辑”的 URL 时,您是否记得执行“编辑”操作?如果没有编辑操作,您将收到 404。

Well, for starters, that id1="" line is going to be problematic, because you can't make something optional that's not at the end.

I just tried it on my system, and it works just fine.

This is the route:

routes.MapRoute(
    "shelf-route",
    "shelves/{id1}/products/{action}/{id2}",
    new { controller = "Products", action = "List", id2 = "" }
);

This is the controller:

public class ProductsController : Controller
{
    public string List(string id1, string id2)
    {
        return String.Format("ID1 = {0}, ID2 = {1}", id1, id2);
    }
}

I tried URLs like:

http://localhost:14314/shelves/23/products/list/14
http://localhost:14314/shelves/23/products

And they worked just fine.

When you tried the URL with "edit" in it, did you remember to make an Edit action? If there's no Edit action, you'll get a 404.

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