在路由表中找不到操作?

发布于 2024-08-25 10:04:23 字数 243 浏览 10 评论 0原文

刚刚开始我的第一个 MVC 2.0 .net 应用程序。我设置了一些默认页面,例如:

/Loa/Register
/Loa/About

但是当我请求 /Loa/sdfqsdf (随机字符串)时,我收到 “找不到资源。” 错误,如何解决我可以将这个不存在的操作重定向到默认操作吗?

就像“未找到操作”默认操作一样?

谢谢!

Just started my first MVC 2.0 .net application. And I set up some default pages like:

/Loa/Register
/Loa/About

But when I request for /Loa/sdfqsdf (random string) I get the "The resource cannot be found." error, how can I redirect this non-existing action to a default action?

Like an "action not found" default action?

thx!

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

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

发布评论

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

评论(1

断舍离 2024-09-01 10:04:23

使用路由

您可以定义多个路由(这在现实生活中的 MVC 应用程序中也很常见),因为某些路由具有与默认设置不同的特定设置。尤其是如果你想做体面的搜索引擎优化。

routes.MapRoute(
    "DefaultRoute",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = string.Empty },
    new { action = "Register|Index|About" } // route constraint that limits the actions that can be used with this route
);

routes.MapRoute(
    "InvalidRoutes"
    "{*dummy}",
    new { controller = "Home", action = "Nonexisting" }
);

如果您要向路由表添加其他路由,只需确保将 InvalidRoutes 定义为最后一个即可。

Using routes

You can define more than one route (which is also quite common in real-life MVC applications), because some routes have particular settings that differ from the default one. And especially if you want to do decent SEO.

routes.MapRoute(
    "DefaultRoute",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = string.Empty },
    new { action = "Register|Index|About" } // route constraint that limits the actions that can be used with this route
);

routes.MapRoute(
    "InvalidRoutes"
    "{*dummy}",
    new { controller = "Home", action = "Nonexisting" }
);

If you'll add additional routes to our route table, just make sure the InvalidRoutes is defined as the last one.

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